I need to display true or false not the number 1
$permission === 1 ? true: false
nearly right:
$permission= ($permission === 1) ? 'true': 'false';
echo $permission;
http://php.net/manual/en/language.operators.comparison.php
First you dont make any assignment =
then you have to make a comparison $permission === 1
then you want the string values "true" or "false"
change the boolean to string value
$permission === 1 ? 'true': 'false';
Its as simple as
echo "Output : ".($permission == 1) ? 'true': 'false';
Related
I have a var which return TRUE or FALSE. how do I use ternary logic to return the string
"YES" if $var1 is TRUE and
"NO" if $var1 is FALSE ?
$Var1 = TRUE; /*dynamic value*/
$status = ($Var1 == true ? $Var1:"NO");
Thank you.
$status = $Var1 ? 'YES' : 'NO';
You cannot go shorter than that :)
you do not need neither () nor == TRUE since if $var1 returns true if it is not null, 0 or false.
$status = ($Var1 == true ? "YES":"NO");
$status = ( $var1 === true ) ? "YES" : "NO";
Here is my sample code:
$issue_id = $_POST['issue_id'];
if(!empty($issue_id)){
echo 'true';
}
else{
echo 'false';
}
If I pass 0 to $_POST['issue_id'] by form submitting then it echo false. Which I want is: Condition will be true if the following conditions are fulfilled:
1. true when I pass any value having 0.
2. false when I don't pass any value. i.e: $_POST['issue_id'] is undefined.
I also tried this:
if(!isset($issue_id)){
echo 'true';
}
else{
echo 'false';
}
if(!empty($issue_id) || $issue==0){
echo 'true';
}
else{
echo 'false';
}
The last one is okay, meaning if I pass any value having ZERO then it will echo true. But it will also echo true if I don't pass any value. Any idea?
The last is okay, meaning if I pass any value having ZERO then it echo true. But it also echo true if I don't pass any value. Any idea?
if (isset($_POST["issue_id"]) && $_POST["issue_id"] !== "") {
}
please notice I used !== not !=. this is why:
0 == "" // true
0 === "" // false
See more at http://php.net/manual/en/language.operators.comparison.php
also if you are expecting number you can use
if (isset($_POST["issue_id"]) && is_numeric($_POST["issue_id"])) {
}
since is_numeric("") returns false
http://php.net/manual/en/function.is-numeric.php
Alternatively if you expect number good option is filter_var
if (isset($_POST["issue_id"]) {
$issue_id = filter_var($_POST["issue_id"], FILTER_VALIDATE_INT);
if ($issue_id !== false) {
}
}
since filter_var("", FILTER_VALIDATE_INT) will returns false and filter_var("0", FILTER_VALIDATE_INT) will return (int) 0
http://php.net/manual/en/function.filter-var.php
if(isset($_POST['issue_id'])) {
if($_POST['issue_id'] == 0) {
echo "true";
}
else {
echo "false";
}
}
When you get data from a form, remember:
All text boxes, whether input or textarea will come as strings. That includes empty text boxes, and text boxes which contain numbers.
All selected buttons will have a value, but buttons which are not selected will not be present at all. This includes radio buttons, check boxes and actual buttons.
This means that $_POST['issue_id'] will be the string '0', which is actually truthy.
If you need it to be an integer, use something like: $issue_id=intval($_POST['issue_id']);
#Abdus Sattar Bhuiyan you can also full fill your two condition like below one:
<?php
$_POST["issue_id"] = "0";
$issue_id = isset($_POST['issue_id']) ? (!empty($_POST['issue_id']) || $_POST['issue_id'] === 0 || $_POST['issue_id'] === "0") ? true : false : false;
if($issue_id){
echo 'true';
}
else{
echo 'false';
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the PHP ? : operator called and what does it do?
i was interviewed with a very basic question of PHP which was like:
echo ('True' ? (true ? 't' : 'f') : 'False');
Can Someone explain the details of the output it will yield?
Thanks
This will echo t.
Because of first it will check the first condition that will give true.
and after that in next condition it again give true and execute the first condition that is t.
In if and else condition it will be write as follow:
if('True') { //condition true and go to in this block
if(true){ //condition true and go to in this block
echo 't'; // echo t
} else {
echo 'f';
}
} else {
echo 'False';
}
Looking at this version should make it clear:
if('True'){ // evaluates true
if(true){ // evaluates tre
echo 't'; // is echo'd
}else{
echo 'f';
}
}else {
echo 'False';
}
A non-empty string is considered a truthy value except the string "0". PHP evaluates
'True' ? (true ? 't' : 'f') : 'False'
from left to right as follows:
The expression 'True' is evaluated as a boolean true
Next, the expression following the ? is evaluated
The expression true is... true!
The expression following the ? is returned, which is t
Surprisingly, you'll still get t if the expression was:
echo 'False' ? (true ? 't' : 'f') : 'False'
As 'True' is Evaluated as true
if('True'){
if(true){
echo 't';
}else{
echo 'f';
}
}else{
echo 'False';
}
The inner bracket will be executed first true?'t':'f' it will return 't' that is a string
Now outer condition will check for echo ('True' ? 't' : 'False'). Here 'True' is a "non empty string"(implicitly casted to TRUE), so it evaluate to true, and will return 't'.
Final code will be echo ('t') which will simply echo t
echo ( // will echo the output
'True'? // first ternary operation 'True' is considered true
(true? 't':'f') // will go here for the second ternary operation
// true is also evaluated as true so it will echo 't'
: 'False'); // never goes here
This:
'True'?(true?'t':'f'):'False'
May be written as
// Will always be true if String is not an Empty string.
if('True'){
// if (true) will always enter
if(true){
// t will be the output
echo 't';
}else{
echo 'f';
}
else {
echo 'False';
}
if(0 == ('Pictures'))
{
echo 'true';
}
why it's giving me 'true' ?
Your string will be evaluated as an Integer, so becomes 0, use this : 0 === 'Pictures' that verifies identity (same value and same type)
Check PHP type comparison tables to understand how comparison operators behave in PHP.
In your case, 'Pictures' becomes "0" and therefore 0 = 0.
Let's check following example:
echo (int)'Pictures'; // 0 => 'Picture' as int
echo 0 == 'Pictures'; // 1 => true, 0 = 0
Use:
if (0 === 'Pictures')
{
echo 'true';
}
The === is strict type operator, it not only checks the value but the type as well.
Quick Test:
if(0 == 'Pictures')
{
echo 'true';
}
else
{
echo 'false';
}
outputs true but:
if(0 === 'Pictures')
{
echo 'true';
}
else
{
echo 'false';
}
outputs false
I am trying to set a flag to show or hide a page element, but it always displays even when the expression is false.
$canMerge = ($condition1 && $condition2) ? 'true' : 'false';
...
<?php if ($canMerge) { ?>Stuff<?php } ?>
What's up?
This is broken because 'false' as a string will evaluate to true as a boolean.
However, this is an unneeded ternary expression, because the resulting values are simple true and false. This would be equivalent:
$canMerge = ($condition1 && $condition2);
The value of 'false' is true. You need to remove the quotes:
$canMerge = ($condition1 && $condition2) ? true : false;
Seems to me a reasonable question especially because of the discrepancy in the way PHP works.
For instance, the following code will output 'its false'
$a = '0';
if($a)
{
echo 'its true';
}
else
{
echo 'its false';
}
You are using 'true' and 'false' as string. Using a string(non-empty and not '0' and not ' ', because these are empty strings and will be assume as false) as a condition will results the condition to be true.
I will write some correct conditions that could be use:
$canMerge = ($condition1 && $condition2);
$canMerge = ($condition1 && $condition2) ? true : false;
$canMerge = ($condition1 && $condition2) ? 'true' : 'false';
...
<?php if ($canMerge == 'true') { ?>Stuff<?php } ?>
$canMerge = ($condition1 && $condition2);
then
if ($canMerge){
echo "Stuff";
}