if ($fromcat == "true" || $fromcat == 1){
error_log("binding to CAT " . $cat . " because pfc was " . $fromcat);
} else {
error_log("binding to ROOM " . $room . " because pfc was " . $fromcat);
}
please have a look at the code above.
I think everybody agree that if $fromcat is 0 there's no way I could get a "binding to CAT (...)" message in my console.
And yet, here it is:
[01-Apr-2013 22:34:50] binding to CAT single because pfc was 0
How is that even possible? You can't display the word CAT and the number 0 at the same time! Is PHP drunk?
Use the identical (===) comparison operator.
if ($fromcat === "true" || $fromcat === 1) {
error_log("binding to CAT " . $cat . " because pfc was " . $fromcat);
} else {
error_log("binding to ROOM " . $room . " because pfc was " . $fromcat);
}
You are attempting to compare $fromcat to the string "true". If you wanted to compare it to the boolean true, you would use $fromcat == true. Otherwise:
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.
Documentation
It's also a good idea to reverse the order of your condition.
So
if ($fromcat === "true" || $fromcat === 1)
becomes
if ('true' === $fromcat || 1 === $fromcat)
Reason: PHP - reversed order in if statement
Related
Hello i am trying to add if else statement in array().
here is my code
$excelData = array(
$users->name,
$users->first_name . ' ' . $organization->last_name,
$users->user_email,
date('d M y', $timestamp),
if ($users->amount == NULL) {echo 0;} else { $users->amount; } ,
if($users->coupon_code == NUll) { echo "No Coupon Code";}else {$users->coupon_code;} ,
);
If/else structures are not meant to be used in-line like that. They don't produce a value, but rather they perform an operation. What you're looking to do is have a single expression which produces a value. The ternary conditional operator can be used for that. (Scroll down on that link to the section titled "Ternary Operator".)
For example, this expression produces a value:
$users->amount == NULL ? 0 : $users->amount
It will evaluate to either 0 or $users->amount based on the condition. So in your code you would have:
$excelData = array(
$users->name,
$users->first_name . ' ' . $organization->last_name,
$users->user_email,
date('d M y', $timestamp),
$users->amount == NULL ? 0 : $users->amount,
$users->coupon_code == NULL ? "No Coupon Code" : $users->coupon_code
);
I have a string called $DiscountDescription that can sometimes be populated with data like this:
A43544675, A33540055,
Or like this:
A43544675,
Basically, I can have either one value or two values within it, separated by a comma. I really only need the first value. If there's only one value, it always has a comma and a space after it, which is why in my code below I'm removing the comma and space to evaluate the string.
My current code is below. You can see where I'm only accounting for this if there's one value in the string, but not both. So what I'd like to do is find the comma, and grab everything to the left of the comma, and make that equal to $DiscountDescriptionTrimmed.
$DiscountDescription = $_order->getDiscountDescription();
$DiscountDescriptionTrimmed = substr_replace($DiscountDescription ,"",-2);
if ($DiscountDescriptionTrimmed != '') {
if (substr($DiscountDescriptionTrimmed,0,1) === "e" && strlen($DiscountDescriptionTrimmed) === 11){
$_order->setDiscountDescription('Gift Cards ' . $DiscountDescription);
}
elseif (substr($DiscountDescriptionTrimmed,0,1) === "E" && strlen($DiscountDescriptionTrimmed) === 9){
$_order->setDiscountDescription('Gift Cards ' . $DiscountDescription);
}
elseif (substr($DiscountDescriptionTrimmed,0,1) === "A" && strlen($DiscountDescriptionTrimmed) === 9){
$_order->setDiscountDescription('Gift Cards ' . $DiscountDescription);
}
elseif (strlen($DiscountDescriptionTrimmed) === 17 && substr_count($DiscountDescriptionTrimmed,'-') === 2){
$_order->setDiscountDescription('Gift Cards ' . $DiscountDescription);
}
elseif (strlen($DiscountDescriptionTrimmed) === 8 && ctype_digit($DiscountDescriptionTrimmed)){
$_order->setDiscountDescription('Gift Cards ' . $DiscountDescription);
}
}
You can use strtok() to achieve this:
$DiscountDescriptionTrimmed = strtok($DiscountDescription, ', ');
If you ever needed the second value, you can call strtok() again:
$SecondDiscountDescriptionTrimmed = strtok(', ');
The first line gives the length of first value and second line extracts it.
$length = strpos($DiscountDescription, ',') + 1;
$DiscountDescriptionTrimmed = substr($DiscountDescription, 0, $length);
What about this with simple explode() and getting the 0 th index ?
$DiscountDescription1 = 'A43544675, A33540055,';
$DiscountDescription2 = 'A43544675,';
echo explode(',',$DiscountDescription1)[0];
echo "\n";
echo explode(',',$DiscountDescription2)[0];
Demo : https://eval.in/922189
I am new to PHP and I am struggling to see why my code is not working when the echo displays 0. The code works for everything else however whenever the random operator chooses 0 it outputs 0 Negative, when I have it to display as Zero. I have no idea why it is doing this and I would appreciate any guidance.
$randomNumber = rand(-5, 5);
$var = $randomNumber;
echo $randomNumber;
echo $integerValue = (($var === 0) ? " Zero" : ($var <=-1) ? " Negative" : (($var >=1) ? " Positive" : " Not Positive or Zero") );
The problem is due to PHP ternary operator precedence, which works backwards compared to most other languages:
Understanding nested PHP ternary operator
Try this last line:
echo $integerValue = (($var === 0) ? " Zero" : (($var <=-1)
? " Negative" : (($var >=1)
? " Positive" : " Not Positive or Zero") ));
You are missing a parentheses:
<?php
$randomNumber = rand(-5, 5);
$var = $randomNumber;
echo $randomNumber;
echo $integerValue = (($var === 0) ? " Zero" : (($var <=-1) ? " Negative" : (($var >=1) ? " Positive" : " Not Positive or Zero") ) );
Explanation:
(
($var === 0) ? " Zero" : (
($var <=-1) ? " Negative" : (
($var >=1) ? " Positive" : " Not Positive or Zero"
)
)
)
You don't want to echo the assignment. You need to assign the value, then echo it. Try changing the last line to
$integerValue = (($var === 0) ? " Zero" : ($var <=-1) ? " Negative" : (($var >=1) ? " Positive" : " Not Positive or Zero") );
and then adding the line
echo $integerValue;
You already have another answer that explains why you weren't getting the outcome you expected, but just FYI, your multiple ternary expression can be simplified somewhat.
echo $var ? $var > 0 ? ' Positive' : ' Negative' : ' Zero';
$var will evaluate to false if it is zero so there's no need to explicitly compare it to zero, and the final "Not Positive or Zero" isn't really a possible outcome, as the result of rand will be an integer, which is either positive, negative, or zero by definition.
Parentheses aren't necessary, but they may make it more obvious what the expression is doing.
echo $var ? ($var > 0 ? ' Positive' : ' Negative') : ' Zero';
I'm using the following function to turn my numbers into 2 decimals with negatives in parenthesis.This almost works
However as I have them right aligned they don't quite line up as i'd like.
The ) sits above the 3. It would be better if the 4 and 3 aligned above each other.
(11,870.74)
2,806.33
Is any of this possible?
function myformat($nr)
{
$nr = number_format($nr, 2);
return $nr[0] == '-' ? "(" . substr($nr, 1) . ")" : $nr;
}
Add a space at the end of the positive number, to match the close parenthesis:
function myformat($nr)
{
$nr = number_format($nr, 2);
return $nr[0] == '-' ? "(" . substr($nr, 1) . ")" : $nr . ' ';
}
I assume you're using printf() in the caller to right-align the results.
I have an array. Consider that $a['info'] == 5. So I can write :
<?php echo $a['info'] . ' children'; ?> to get : "5 children".
Normally, i could write
<?php echo (isset($a['info']) ? $a['info'] : '0') . ' children'; ?>
to get "0 children" if $a['info'] == 0 or is not set. But
<?php echo ($a['info'] | '0') . ' children'; ?> works too, but I don't know why.
Thank you very much.
EDIT : works too with $a['info'] ?: '0'.
Pipe is byte or operation, so
$val | 0
Equals to
$val ? $val : 0
But this code dont check if variable exists, so if it's not - NOTICE will be raised, but code will work too, becose PHP boolean cast specifications.
isset checks if variable exists, if it exits, even if it value is 0, isset return true, so code
echo (isset($a['info']) ? $a['info'] : '0') . ' children';
echo "0 children" if variable not exits, not if it value is 0.
echo ($a['info'] | '0')
This is nothing but an OR operation , it will OR like this , 0 OR 0 = 0 , 5 OR 0 = 5 , so you will get the right answer .
A single pipe is nothing but OR operation .
Bitwise OR