Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Ternary operator ('?') weirdness

Ternary operator ('?') weirdness

Ternary operator ('?') weirdness

I have another dumb question. smile 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:


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:


<li><a class="addbutton" id="addnew" href="itemsetup.php?clientid=1">Add New Top-Level Item</a></li> 

...but instead it's returning this:


<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?


RE: Ternary operator ('?') weirdness

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.

RE: Ternary operator ('?') weirdness

Oh, right! Thanks! smile

(VBA alum, new to PHP)


RE: Ternary operator ('?') weirdness

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close