Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ternary operator ('?') weirdness 1

Status
Not open for further replies.

Katerine

Programmer
Mar 9, 2001
234
US
Hi,
I have another dumb question. :) I hope I'm just missing something obvious, but I've been looking at this syntax for a while, and I'm just not seeing it.

Running this on my local server, Win 10 with IIS running PHP 5.6.

The following variables are set as follows:
$locationid = 0;
$parentitemid = 0;

I then have the following line of code:
PHP:
echo '<li><a class="addbutton" id="addnew" href="itemsetup.php?clientid=' . $clientid . 
(($locationid=0) ? '' : '&locationid=' . $locationid) . 
(($parentitemid=0) ? '' : '&parentitemid=' . $parentitemid) . '">Add New Top-Level Item</a></li>';
As far as I can tell, that line of code SHOULD return this HTML:
HTML:
<li><a class="addbutton" id="addnew" href="itemsetup.php?clientid=1">Add New Top-Level Item</a></li>

...but instead it's returning this:
HTML:
<li><a class="addbutton" id="addnew" href="itemsetup.php?clientid=1&locationid=0&parentitemid=0">Add New Top-Level Item</a></li>

What am I doing wrong?

Katie
 
assignment: $var=value
compare: if ($var==value) ifcode; or ($var==value) ? ifvalue : elsevalue;

You need two = for comparisons, or even === for strict type comparisons.

Bye, Olaf.
 
Oh, right! Thanks! :)

(VBA alum, new to PHP)

Katie
 
Those are the things every language handles slightly different.

Since a single = is correct syntax, though seldom useful in if conditions. If you're using phpStorm you can activate highlighting assigments in conditions via Code Inspection settings, by default this is not highlighted, but you just need to set the option.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top