Welcome Guest, Not a member yet? Register   Sign In
!$variable
#1

[eluser]Kemik[/eluser]
Hello,

I'm setting the $header variable for all my page titles. However, I want to add a line in the header which says, if the $header variable doesn't exist then do the default title of "My Website" instead of "Page :: Website".

If I use the current code:

Code:
<?php if (!$header): ?>
<title>My Website</title>
<?php else: ?>
<title><?php echo $header; ?> :: Website</title>
<?php endif; ?>

I get the "Variable doesn't exist" error at the top of the page. Is there anyway around this or is it simply a case of "You better not forget to declare the $data['header'] variable in every page!"

Thanks.
#2

[eluser]alexsancho[/eluser]
You can use this instead

Code:
<?php if(isset($header) AND $header != '');?>
#3

[eluser]marcoss[/eluser]
One line solution,

Code:
<title><?=(isset($header))?$header:'My Website'?></title>
#4

[eluser]John_Betong[/eluser]
And yet another alternative from the PHP manual Smile

Code:
<?php
$var = 0;

// Evaluates to true because $var is empty
if (empty($var)) {
    echo '$var is either 0, empty, or not set at all';
}

// Evaluates as true because $var is set
if (isset($var)) {
    echo '$var is set even though it is empty';
}
?>
 
Cheers,
 
John_Betong
 
 
#5

[eluser]marcoss[/eluser]
[quote author="John_Betong" date="1187596075"]And yet another alternative from the PHP manual Smile

Code:
// Evaluates to true because $var is empty
if (empty($var)) {
    echo '$var is either 0, empty, or not set at all';
}
[/quote]

In the case $var is not set, this will throw and error. So in this case, the example is not suitable, you need to check for isset() first .
#6

[eluser]John_Betong[/eluser]
Hi Marcoss,

Can you run this code on your computer and let me know the result?

Quote: <div style='position:fixed; left:0; top:0; background:#fff none; color:#000'>
&lt;?php
// $fred is not declared
echo heading (empty($fred) ? 'empty($fred) or ! isset($fred)' : 'Yes $fred ==> ' .$fred, 5);

// declare $fred
$fred = 'this string';
echo heading (empty($fred) ? 'empty($fred) or ! isset($fred)' : 'Yes $fred ==> ' .$fred, 5);
?&gt;
</div>
&nbsp;
I am using PHP5 if it makes any difference.
&nbsp;
Cheers,
&nbsp;
John_Betong
&nbsp;
#7

[eluser]marcoss[/eluser]
No need to run it, that won't work, you have some syntax errors in there.

What are you trying to do?
#8

[eluser]John_Betong[/eluser]
Hi Marcoss,

I was just giving an example to show that the isset(...) function is built-in to empty(...) function.
&nbsp;
The code supplied has no syntax errors. It was tested on my computer and pasted into the line immediately below &lt;body...&gt;.
Code:
<div style=’position:fixed; left:0; top:0; background:#fff none; color:#000’>
&lt;?php
    // $fred is not declared
    echo heading (empty($fred) ? 'empty($fred) or ! isset($fred)' : 'Yes $fred ==> ' .$fred, 5);

    // declare $fred
    $fred = 'this string';
    echo heading (empty($fred) ? 'empty($fred) or ! isset($fred)' : 'Yes $fred ==> ' .$fred, 5);
  ?&gt;
</div>
&nbsp;
As I mentioned before, give it a try and let me know the results, especially if there are any errors Smile
&nbsp;
Cheers,
&nbsp;
John_Betong
&nbsp;
&nbsp;
#9

[eluser]marcoss[/eluser]
Ok, no syntax error, maybe the lack of syntax highlighting helped me misread it, anyway, heading() is not a function i could test.

Regarding the use of empty, it's wrong, because empty() is suppose to check whether a variable is empty or not, if you have no variable (not set), then you have nothing to check.

Quote:empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set.

So use isset() if you want to actually check is the variable is or not set.

Regarding the above example you cited form the php site, it's using empty() against an already defined variable ($var=0).
#10

[eluser]John_Betong[/eluser]
Hi Marcoss,

>>> ...anyway, heading() is not a function i could test.
Check the CI Help for "HTML Helper".
&nbsp;
>>> Regarding the use of empty, it’s wrong,
>>> because empty() is suppose to check whether a variable is empty or not,
>>> if you have no variable (not set), then you have nothing to check.
The PHP Manual states that
// Evaluates to true because $var is "either 0, empty, or not set at all';
&nbsp;
Revised code:
Code:
<div style='position:fixed; left:0; top:0; background:#fff none; color:#000'>

  <h5>
    &lt;?= empty($fred) ? 'empty($fred) or ! isset($fred)' : 'Yes $fred ==> ' ?&gt;
  </h5>

  <h5>
    &lt;?= $fred='THIS STRING '; empty($fred) ? 'empty($fred) or ! isset($fred)' : 'Yes $fred ==> ' ?&gt;
  </h5>

  
</div>
&nbsp;
Cheers,
&nbsp;
John_Betong
&nbsp;




Theme © iAndrew 2016 - Forum software by © MyBB