Jump to content

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


asker54

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

Link to comment
Share on other sites

<?= 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'.
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.

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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.