In order to fill a tab with lots of values, I would like not to display values which are equal to '0'.
For example, instead of having :
Test USER 1 0 1 0
Sample USER 1 0 1 0
I'd like to have :
Test USER 1 1
Sample USER 1 1
I display my values using printf("<td>%d</td>", $value);
I've even tried printf("<td>%0d</td>", $dl_ends[0]);
Thanks
<td><?php $value ? printf("%d", $value) : ''; ?></td>
There are two ways to solve my problem :
printf("<td>%s</td>", $value != 0 ? $value : ''); which avoid number formatting, or
$value != 0 ? printf("<td>%d</td>", $value) : printf("<td></td>"); which is longer but more precise.
Try this
printf("<td>%d</td>", $value != 0 ? $value : '');
EDIT
If you also need to catch empty strings, you can use the following code
printf("<td>%d</td>", $value != 0 && $value != '' ? $value : '');
Related
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 am working on a project and then I got this problem. Here is the scenario, when I insert in database, it must not insert if the value is zero ,,but then when I check my database ,there are zeros being inserted, I don't know where I go wrong or I just missed the trapping that if the value is equal zero then it must not insert.
Here is the code :
$ref_array = explode(',' , $ref_number);
$po_array = explode(',' , $po_number);
$inv_array = explode(',' , $inv_number);
$asn_array = explode(',' , $asn_number);
$adj_array = explode(',' , $adj_number);
$amount_array = explode(',' , $amount);
// count the number of po,invoice,asn and adj
if(count($po_array) != count($ref_array) || count($inv_array) != count($ref_array) || count($asn_array) != count($ref_array) || count($adj_array) != count($ref_array) || count($ref_array) != count($amount_array)){
foreach ($ref_array as $i => $ref_num){
$po_num = isset($po_array[$i]) ? $po_array[$i] : '' ; //leave blank there is no $po_array[$i]
$inv_num = isset($inv_array[$i]) ? $inv_array[$i] : '';
$asn_num = isset($asn_array[$i]) ? $asn_array[$i] : '' ;
$adj_num = isset($adj_array[$i]) ? $adj_array[$i] : '' ;
$amount_num = isset($amount_array[$i])? $amount_array[$i] : '';
if(intval($ref_num) != 0 ){
$conn->query ("INSERT INTO transaction_detail (`transaction_id`,`ref_number`,`po_number`,`inv_number`,`asn_number`,`adj_number`,`amount`)
VALUES ('$transaction_id','$ref_num','$po_num','$inv_num','$asn_num','$adj_num','$amount_num') " );
}
}
}
Can somebody help me?
You just remove intval form your condition because it is use to Get the integer value of a variable.
Second check $ref_num variable is greater then zero
you condition would be
if(($ref_num) >0 ){
Also read intval()
Check the transaction_detail table whether you have put default value as 0 for po_number field.
I'd like to to some thing similar to JavaScript's
var foo = true;
foo && doSometing();
but this doesn't seem to work in PHP.
I'm trying to add a class to a label if a condition is met and I'd prefer to keep the embedded PHP down to a minimum for the sake of readability.
So far I've got:
<?php $redText='redtext ';?>
<label class="<?php if ($requestVars->_name=='')echo $redText;?>labellong">_name*</label>
<input name="_name" value="<?php echo $requestVars->_name; ?>"/>
but even then the IDE is complaining that I have an if statement without braces.
use the ternary operator ?:
change this
<?php if ($requestVars->_name == '') echo $redText; ?>
with
<?php echo ($requestVars->_name == '') ? $redText : ''; ?>
In short
// (Condition)?(thing's to do if condition true):(thing's to do if condition false);
You can use Ternary operator logic
Ternary operator logic is the process of using "(condition)? (true return value) : (false return value)" statements to shorten your if/else structures. i.e
/* most basic usage */
$var = 5;
$var_is_greater_than_two = ($var > 2 ? true : false); // returns true
Something like this?
($var > 2 ? echo "greater" : echo "smaller")
I like to use the minimalist PHP text output syntax:
HTML stuff <?= $some_string ?> HTML stuff
(This works the same as using an <?php echo $some_string; ?>)
You can also use the ternary operator:
//(condition) ? (do_something_when_true) : (do_something_when_false);
($my_var == true) ? "It's true" : "It's false ;
Ending up like this:
<?= ($requestVars->_name=='') ? $redText : '' ?>
Sample Usage
Here are a couple more uses of ternary operators, ranging from simple to advanced:
Basic Usage:
$message = 'Hello '.($user->is_logged_in() ? $user->get('first_name') : 'Guest');
Short hand Usage:
$message = 'Hello '.($user->get('first_name') ?: 'Guest');
Echo Inline
echo 'Based on your score, you are a ',($score > 10 ? 'genius' : 'nobody');
A bit Tougher
$score = 10;
$age = 20;
echo 'Taking into account your age and score, you are: ',($age > 10 ? ($score < 80 ? 'behind' : 'above average') : ($score < 50 ? 'behind' : 'above average')); // returns 'You are behind'
complicated level
$days = ($month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year %400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31)); //returns days in the given month
To learn more about ternary operators and usage, visit PHP.net Comparison Operators or here.
Use ternary operator:
echo (($test == '') ? $redText : '');
echo $test == '' ? $redText : ''; //removed parenthesis
But in this case you can't use shorter reversed version because it will return bool(true) in first condition.
echo (($test != '') ?: $redText); //this will not work properly for this case
Ill provide with an other answer since the original question specifies the use of if() in html
<a class="menu-item" href="/about-us"><?= (pll_current_language() == 'en') ? 'About us' : 'Om oss' ?></a>
The provided answers are the best solution in your case, and they are what I do as well, but if your text is printed by a function or class method you could do the same as in Javascript as well
function hello(){
echo 'HELLO';
}
$print = true;
$print && hello();
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
$n=21;
$p=$n%10==1 && $n%100!=11 ? 0 : $n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20) ? 1 : 2;
why is $p = 2?
it is supposed to be $p = 0!
is it a bug or am I missing something?
I got this from trying to get the plural form for Russian on: http://www.gnu.org/s/hello/manual/gettext/Plural-forms.html
should be this one:
$p=($n%10==1 && $n%100!=11) ? 0 : (($n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20)) ? 1 : 2);
the error was in missing bracets
you can see here: http://php.net/manual/en/language.operators.comparison.php that "It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious". You can see that if you enclose the else part for the first if between ( and ) you will get another result:
$p=$n%10==1 && $n%100!=11 ? 0 : ($n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20) ? 1 : 2);
Maybe you should consider changing you statement to a "regular" if block, something like:
if ($n%10==1 && $n%100!=11)
{
$p =0 ;
}
elseif ($n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20))
{
$p = 1;
}
else
{
$p= 2;
}
this way being easier to read