This question already has answers here:
In php, is 0 treated as empty?
(18 answers)
Closed 6 years ago.
I recently started writing a website for learning html/css/js/php.
I designed the front-end of my site with bootstrap.
Now I am trying to validate some inputs with PHP.
I tried this:
if ($durationHH <= 0 && $durationMM <= 0)
{
echo "DurationHH and DurationMM can not be both zero at the same time.";
echo "<br>";
echo "DurationHH and DurationMM can not be smaller than 0.";
}
elseif (empty($durationHH) || empty($durationMM))
{
echo "DurationHH and DurationMM can not be empty.";
echo "<br>";
}
else
{
echo $_POST["durationMM"];
echo ":";
echo $_POST["durationHH"];
}
I tested the validation by putting in some values for durationHH and durationMM.
Everything is working fine so far, except these two cases:
durationMM = 0 AND durationHH = any value
&&
durationHH= 0 AND durationMM = any value.
In these two cases i get the output: "DurationHH and DurationMM can not be empty."
How/why does this happen?
if durationHH and durationMM are always required to be integers or numeric, use ! is_numeric() to check.
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I have got following code in php:
$canPrice = 4.98;
$insertedMoney = 5;
$remainingRest = $insertedMoney - $canPrice;
echo $remainingRest;
if( $remainingRest == 0.02 )
echo "equal";
else
echo "not equal".$remainingRest;
nothing more, it should print equal but somehow it's printing not equal, through I see, $remainingRest is equal 0.02.
Thanks for help in advance
Returns false because float values don't equal exactly when equalize.
You can try to equalize;
<?php
$canPrice = 4.98;
$insertedMoney = 5;
$remainingRest = $insertedMoney - $canPrice;
if(round($remainingRest,2) == round(0.02,2))
echo "equal";
else
echo "not equal".$remainingRest;
?>
This question already has answers here:
What is the most efficient way to count all the occurrences of a specific character in a PHP string?
(4 answers)
Closed 5 years ago.
I am trying to separate 2 symbols from a string and then
count how many of these symbols there are.
So if i had : 1110001110000
Then it should find that there are 6= 1's and 7= 0's
So This is what I have tried:
Essentially what I need, is to read the string indexes in code would be string[$i] then IF there is a 1 or a 0 count it.
I tried using a for-loop
for ($i=0; $i < $getInput[$i] ; $i++) {
if ($getInput[$i] == 1) {
echo "ONE";
} elseif ($getInput[$i] == 0) {
echo "ZERO";
}
}
Here im trying to echo out ONE for everytime ther is a 1 and ZERO for everytime theres a zero.
$counter = 0;
foreach ($getInput as $key) {
echo $key;
}
here i tried to utilize a foreach, here I am not really declaring to see for One index, i tried putting a for each in a for but needless to say, it didn't work.
Using substr_count, you can do this in a fairly straightforward way:
echo substr_count("1110001110000", '1'); //Echos 6
echo substr_count("1110001110000", '0'); //Echos 7
Substr_count.
http://php.net/manual/en/function.substr-count.php
$str = "1110001110000";
Echo "there is " .substr_count($str, "0") ." zeros \n";
Echo "there is " .substr_count($str, "1") ." ones \n";
https://3v4l.org/BQEiR
If you want to output it as your code implies (one one one zero) you can use numberformatter.
Here I split the string to an array and loop through it and output the spellout of each number.
$str = "1110001110000";
$arr = str_split($str);
$nf = new NumberFormatter("en", NumberFormatter::SPELLOUT);
Foreach($arr as $numb){
echo $nf->format($numb) . " ";
}
Output:
one one one zero zero zero one one one zero zero zero zero
https://3v4l.org/nHX59
This question already has answers here:
Browser white space rendering
(3 answers)
Closed 5 years ago.
I am trying to print the letter 'A' using star pattern but somewhere I am getting wrong. The code is as follow-
<?php
for ($row=0; $row<=7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ((($column == 1 or $column == 5) and $row != 0) or (($row == 0 or $row == 3) and ($column > 1 and $column < 5)))
echo "*";
else
echo " ";
}
echo "<br>";
}
?>
The letter A does not appear in proper format.
You shouldn't use spaces. CHoose another character or use , which is an unbreakable space.
Web browsers remove consecutive spaces so that there is only one space between two non-spaces characters. That's why your display looks broken.
Unbreakable spaces will be printed no matter what.
Just add in your else block echo " ";
This question already has answers here:
How exactly does if($variable) work? [duplicate]
(8 answers)
Closed 7 years ago.
This is the code
<p>We are going to flip a coin until we get three heads in a row!</p>
<?php
$headCount = 0;
$flipCount = 0;
while ($headCount < 3) {
$flip = rand(0, 1);
$flipCount ++;
if ($flip) {
$headCount ++;
echo "<div class=\"coin\">H</div>";
} else {
$headCount = 0;
echo "<div class=\"coin\">T</div>";
}
}
echo "<p>It took {$flipCount} flips!</p>";
?>
I've played around with Java but I'm now learning PHP. The part that doesn't make sense right here is this.
if ($flip) {
$headCount ++;
echo "<div class=\"coin\">H</div>";
}
What exactly is being checked here? I doubt it's checking whether $flip is true.
It's a common shorthand in these kinds of languages. Once you get used to it, it seems natural.
In this case,
if ($flip)
is equivalent to:
if ($flip == 1)
But PHP is really just checking for a "truthy" value: true, not zero, not "0" or "", not [], and others will evaluate as true and pass the if ($flip) test.
This question already has answers here:
Compare floats in php
(17 answers)
Closed 9 years ago.
hello I have a problem wtich two variables in php. i have this code:
var_dump($total);echo '<br/>';
var_dump($reserva->getAdelanto());
if ($total == $reserva->getAdelanto()){
$total = 0;
echo "hello";
}
else
$total = $total - $reserva->getAdelanto();
print :
float(3940.2)
float(3940.2)
but does not enter the if when the two variables are equal. anyone knows why is that? greetings and thanks.
May be try with abs like
if ((abs($a)-abs($b)) <= 0.00001) {
echo "same";
}
Or
if (abs($a - $b) <= 0.00001) {
echo "same";
}
Or you also try like
var_dump( bccomp($a, $b) == 0 )
returns true if they are same