Jump to content

What does this statement means (<?=) in a tenary operator


asker54
Go to solution Solved by trq,

Recommended Posts

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 by asker54
Link to comment
Share on other sites

@kicken @Ch0cu3r

 

I also thought like yours, but when I substitute <?= with <?php echo , I get the error

 

Values
Notice: Undefined index: submitted in C:\wamp\www\general\sortingarray.php on line 46
unsorted:

 

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

 

 

 

 
Link to comment
Share on other sites

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 by mac_gyver
Link to comment
Share on other sites

@ trq

 

Thanks. I replaced the the (

 

 

@kicken @Ch0cu3r

 

I also thought like yours, but when I substitute <?= with <?php echo , I get the error

 

Values
Notice: Undefined index: submitted in C:\wamp\www\general\sortingarray.php on line 46
unsorted:

 

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.

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

 

 

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 by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.