Jump to content


Photo

Defining variables


  • Please log in to reply
6 replies to this topic

#1 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 15 September 2006 - 09:39 AM

In an attempt to write good code, I'm going to put
error_reporting(E_ALL);
on all my pages.

I'd like to know how to define a variable.  I thought that you just had to assign something to it?

I'm getting the following error on my page:

Notice: Undefined variable: bg in /home/domains/dizzie.co.uk/user/htdocs/php/pages.php on line 39

Now line 39 looks like this:

$bg = ($bg == "#CCFF99" ? "#CCCC99" : "#CCFF99");

Now the first $bg is attempting to have something assigned to it, I'm assuming it's the 2nd $bg in that row that php has the problem with, but don't know how to get around this.  If I just add
$bg = "null";
on the line before it, it's fine, but is there a better way to do this?

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 15 September 2006 - 09:56 AM

You've formatted your ternary operator incorrectly, it should be:
<?php
$bg = ($bg == "#CCFF99") ? "#CCCC99" : "#CCFF99";
?>


#3 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 15 September 2006 - 10:01 AM

Thanks for that SA, I didn't notice that, but have corrected that now.

Any ideas on my define problem?

Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#4 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 15 September 2006 - 10:08 AM

The ternary operator syntax is correct on both accounts, so it's just personal preference.. it could even be:
$bg = $bg == 'foo' ? 'val1' : 'val2';

the reason you are getting the define error is because you are attempting to use the variable before it has been defined - i.e. your comparison is the problem.

<?php

$bg = isset($bg) 
    ? $bg == '#CCFF99' ? $bg : '#CCCC99'
    : '#CCFF99'
;

?>


#5 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 15 September 2006 - 10:14 AM

Nested ternary operator's huh... I'll have to be careful here, but that worked thanks Jenk.

Can you possibly show me what this would look like if it was a nested 'if' statement, to help me better understand?

Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#6 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 15 September 2006 - 10:27 AM

<?php

if (isset($bg))
{
    if ($bg == '#CCFF99')
    {
        $bg = '#CCCC99';
    }
    else
    {
        $bg = '#CCFF99';
    }
}
else
{
    $bg = '#CCFF99';
}

?>

or shortened to:
<?php

if (!isset($bg) || $bg == '#CCCC99') $bg = '#CCFF99';
else $bg = '#CCCC99';

?>

or in ternary:

<?php

$bg = (!isset($bg) || $bg == '#CCCC99') ? '#CCFF99' : '#CCCC99';

?>


#7 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 15 September 2006 - 10:57 AM

Thanks matey...

That's cleared it up.

Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users