asker54 Posted September 25, 2013 Share Posted September 25, 2013 (edited) I met with the ternary operator statement <p>Values <?= $_POST['submitted'] ? "sorted by {$sortType}" : "unsorted"; ?>:</p> I do understand what the statement mean except the the equality sign after the question mark. I would appreciate it if any one would explain what did it mean. Thanks Edited September 25, 2013 by asker54 Quote Link to comment https://forums.phpfreaks.com/topic/282434-what-does-this-statement-means/ Share on other sites More sharing options...
kicken Posted September 25, 2013 Share Posted September 25, 2013 <?= is just a shortcut way of saying <?php echo, so that line is equal to: <?php echo $_POST['submitted'] ? "sorted by {$sortType}" : "unsorted"; ?> It will echo 'sorted by $sortType' if the post variable exists, otherwise it will echo 'unsorted'. Quote Link to comment https://forums.phpfreaks.com/topic/282434-what-does-this-statement-means/#findComment-1451159 Share on other sites More sharing options...
Ch0cu3r Posted September 25, 2013 Share Posted September 25, 2013 Do note though that the <?= ?> tags will only work if short_open_tag directive is enabled in the php.ini file. Not all servers will have this setting enabled and/or allowed to be changed. Using full PHP tag syntax is recommend (as kicken showed you above). Quote Link to comment https://forums.phpfreaks.com/topic/282434-what-does-this-statement-means/#findComment-1451165 Share on other sites More sharing options...
kicken Posted September 25, 2013 Share Posted September 25, 2013 Do note though that the <?= ?> tags will only work if short_open_tag directive is enabled in the php.ini file. Also note that the above is only true in <5.4. In 5.4.0 and newer, <?= will always work and is no longer tied to short tags. Quote Link to comment https://forums.phpfreaks.com/topic/282434-what-does-this-statement-means/#findComment-1451170 Share on other sites More sharing options...
asker54 Posted September 25, 2013 Author Share Posted September 25, 2013 @kicken @Ch0cu3r I also thought like yours, but when I substitute <?= with <?php echo , I get the error ValuesNotice: Undefined index: submitted in C:\wamp\www\general\sortingarray.php on line 46unsorted: and when I leave it as it is, I get no error. Here is the code I am applying: <?php function userSort($a, $b) { // smarts is all-important, so sort it first if ($b == "smarts") { return 1; } else if ($a == "smarts") { return −1; } return ($a == $b) ? 0 : (($a < $b) ? −1 : 1); } $values = array( 'name' => "Buzz Lightyear", 'email_address' => "buzz@starcommand.gal", 'age' => 32, 'smarts' => "some" ); if ($_POST['submitted']) { $sortType = $_POST['sort_type']; if ($sortType == "usort" || $sortType == "uksort" || $sortType == "uasort") { $sortType($values, "user_sort"); } else { $sortType($values); } } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?> " method="post"> <p> <input type="radio" name="sort_type" value="sort" checked="checked" /> Standard<br /> <input type="radio" name="sort_type" value="rsort" /> Reverse<br /> <input type="radio" name="sort_type" value="usort" /> User-defined<br /> <input type="radio" name="sort_type" value="ksort" /> Key<br /> <input type="radio" name="sort_type" value="krsort" /> Reverse key<br /> <input type="radio" name="sort_type" value="uksort" /> User-defined key<br /> <input type="radio" name="sort_type" value="asort" /> Value<br /> <input type="radio" name="sort_type" value="arsort" /> Reverse value<br /> <input type="radio" name="sort_type" value="uasort" /> User-defined value<br /> </p> <p align="center"><input type="submit" value="Sort" name="submitted" /></p> <p>Values <?php echo $_POST['submitted'] ? "sorted by {$sortType}" : "unsorted"; ?>:</p> <ul> <?php foreach ($values as $key => $value) { echo "<li><b>{$key}</b>: {$value}</li>"; } ?> </ul> </form> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/282434-what-does-this-statement-means/#findComment-1451194 Share on other sites More sharing options...
Solution trq Posted September 25, 2013 Solution Share Posted September 25, 2013 That is a different issue. Your not checking to see that $_POST['submitted'] exists. <?php echo isset($_POST['submitted']) ? "sorted by {$sortType}" : "unsorted"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/282434-what-does-this-statement-means/#findComment-1451230 Share on other sites More sharing options...
mac_gyver Posted September 25, 2013 Share Posted September 25, 2013 (edited) it's likely your version/configuration of php isn't recognizing the <? at all and that specific line of code wasn't being seen as being php code. when you change to the full opening <?php tag, it's now being seen as php code. the correct way of testing a variable that might not exist is to use isset() edit: lol, the forum's pop up about posts being made came after i hit the submit button and listed 2 new posts having been made, trq's and mine. Edited September 25, 2013 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/282434-what-does-this-statement-means/#findComment-1451231 Share on other sites More sharing options...
asker54 Posted September 26, 2013 Author Share Posted September 26, 2013 @ trq Thanks. I replaced the the ( @kicken @Ch0cu3r I also thought like yours, but when I substitute <?= with <?php echo , I get the error ValuesNotice: Undefined index: submitted in C:\wamp\www\general\sortingarray.php on line 46unsorted: and when I leave it as it is, I get no error. Here is the code I am applying: <?php function userSort($a, $b) { // smarts is all-important, so sort it first if ($b == "smarts") { return 1; } else if ($a == "smarts") { return −1; } return ($a == $b) ? 0 : (($a < $b) ? −1 : 1); } $values = array( 'name' => "Buzz Lightyear", 'email_address' => "buzz@starcommand.gal", 'age' => 32, 'smarts' => "some" ); if ($_POST['submitted']) { $sortType = $_POST['sort_type']; if ($sortType == "usort" || $sortType == "uksort" || $sortType == "uasort") { $sortType($values, "user_sort"); } else { $sortType($values); } } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?> " method="post"> <p> <input type="radio" name="sort_type" value="sort" checked="checked" /> Standard<br /> <input type="radio" name="sort_type" value="rsort" /> Reverse<br /> <input type="radio" name="sort_type" value="usort" /> User-defined<br /> <input type="radio" name="sort_type" value="ksort" /> Key<br /> <input type="radio" name="sort_type" value="krsort" /> Reverse key<br /> <input type="radio" name="sort_type" value="uksort" /> User-defined key<br /> <input type="radio" name="sort_type" value="asort" /> Value<br /> <input type="radio" name="sort_type" value="arsort" /> Reverse value<br /> <input type="radio" name="sort_type" value="uasort" /> User-defined value<br /> </p> <p align="center"><input type="submit" value="Sort" name="submitted" /></p> <p>Values <?= $_POST['submitted'] ? "sorted by {$sortType}" : "unsorted"; ?>:</p> <ul> <?php foreach ($values as $key => $value) { echo "<li><b>{$key}</b>: {$value}</li>"; } ?> </ul> </form> Thanks @trq. please see the above quoted text where I replaced the equqlity sign (=) in the original program above with your suggested replacement <p>Values <?php echo isset($_POST['submitted']) ? "sorted by {$sortType}" : "unsorted"; ?>:</p> The statement that I changed in the above quoted program is: <p>Values <?= $_POST['submitted'] ? "sorted by {$sortType}" : "unsorted"; ?>:</p> With both settings, the code works without errors. What I do not understand what does the equality sign represent here, does it equal the isset function here. Quote Link to comment https://forums.phpfreaks.com/topic/282434-what-does-this-statement-means/#findComment-1451281 Share on other sites More sharing options...
Ch0cu3r Posted September 26, 2013 Share Posted September 26, 2013 (edited) With both settings, the code works without errors. What I do not understand what does the equality sign represent here, does it equal the isset function here. No the = in the <?= tags is not the same as the assignment operator. It is just short hand syntax for <?php echo The isset isset($_POST['submitted']) ? "sorted by {$sortType}" : "unsorted"; The above Is returning the string "sorted by {$sortType}" if $_POST['submmited'] exists. If it doesn't exist the string "unsorted" is returned. The <?= tag will echo out the string returned. Edited September 26, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282434-what-does-this-statement-means/#findComment-1451287 Share on other sites More sharing options...
asker54 Posted September 26, 2013 Author Share Posted September 26, 2013 Ch0cu3rThanks. Can you please tell me where can I read about that <?= is equivalent to the syntax <?php echo Quote Link to comment https://forums.phpfreaks.com/topic/282434-what-does-this-statement-means/#findComment-1451326 Share on other sites More sharing options...
kicken Posted September 27, 2013 Share Posted September 27, 2013 Ch0cu3r Thanks. Can you please tell me where can I read about that <?= is equivalent to the syntax <?php echo It's in the manual: Language Reference: PHP Tags: Escaping HTML Quote Link to comment https://forums.phpfreaks.com/topic/282434-what-does-this-statement-means/#findComment-1451340 Share on other sites More sharing options...
asker54 Posted September 28, 2013 Author Share Posted September 28, 2013 It's in the manual: Language Reference: PHP Tags: Escaping HTML Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/282434-what-does-this-statement-means/#findComment-1451617 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.