Here is my code:
<?php
$num1 = 10;
$num2 = 15;
echo "<font color='red'>$num1 + $num2</font>" . "<br>";
?>
I expect it to equal "25", when I add a font color it equals "10 + 15". Why?
You cannot do math inside a string. Instead, close the string. perform your addition operation, and concatenate the result.
echo "<font color='red'>" . ($num1 + $num2) . "</font><br>";
As Orangepill wisely points out, this can be accomplished more efficiently like this:
echo "<font color='red'>",($num1 + $num2),"</font><br>";
change
echo "<font color='red'>$num1 + $num2</font>" . "<br>";
to
echo "<font color='red'>",($num1 + $num2),"</font><br>";
You need to concatenate your string otherwise math will not work
echo "<font color='red'>".($num1 + $num2)."</font>" . "<br>";
Related
I have a Foreach where I have two variables ($num1 and $num2) as string. I'd like to covert them as number (integer should be ok).
This is my code:
...
foreach ($titles as $match) {
list($num1, $num2) = explode(':', $results[$c++]->innertext); // <- explode
echo "<tr><td class='rtitle'>".
"<td class='last-cell'>".$match_dates[$c]->innertext . "</td> " .
//"<td class='first-cell tl'>".$match->innertext."</td> " .
" - ".$match->innertext." ".$num1.':'.$num2 . " " .
"<td class='odds'>".$best_bets[$b++]->attr['data-odd'] . ";" .
"".$odds[$b++]->attr['data-odd'] . ";" .
"".$odds[$b++]->attr['data-odd'] . "</td>" .
"</td></tr><br/>";
}
...
Thanks!
EDIT
Thanks for your answer. I am going to explain and maybe you could help me: I have a a problem with best_bets value, because it's not alway before odds variable, so I was thinking to use "if statement", where I can fix some rules: when $num1 is > $num2, I will show an echo order; when $num1 is = $num2 another echos order and. at least, $num1 is < $num2 another echos order. But to do this, I need to convert $num1 and $num2 to do it. I hope to be clear. Thank you for your helping
You could cast them to an int:
$num1 = (int) $num1;
Or use the intval function:
$num1 = intval($num1);
Hope this helps.
I have this code:
if($tempPrice > 500)
{
echo $tempPrice*0.038;
}
else
{
echo 'NA';
}
What I want to do is put a '£' before the '$tempPrice*0.038' but not before the NA. So when it outputs it outputs '£123' with the tempprice and 'NA' if there isn't a tempprice.
Using the . sign you can echo multiple parts.
echo "£" . ($tempPrice*0.038);
You can just add it to the beginning of your echo statement:
echo '£' . ($tempPrice*0.038);
Learn more about string operators here: http://php.net/manual/en/language.operators.string.php
Just concatenate it in the first if statement. You can also use the single line notation, like so:
<?php
echo ($tempPrice > 500) ? '£' . ($tempPrice*0.038) : 'NA';
Could somebody please tell me why $total_value evaluates to ZERO when either $bawtry_value or $chain_value are non-zero values?
PHP
$bawtry_value = utf8_encode(money('%n', $bawtry * $price));
echo "Bawtry Value";
echo '<br />';
echo $bawtry_value;
echo '<br />';
$chain_value = utf8_encode(money('%n', $chain * $price));
echo "Chain value";
echo '<br />';
echo $chain_value;
echo '<br />';
$total_value = utf8_encode(money('%n', $bawtry_value + $chain_value));
echo "Total value";
echo '<br />';
echo $total_value;
echo '<br />';
Example Output
Result Bawtry 0
Result Chain 2
Total Pairs 2
Bawtry Value
£0.00
Chain value
£39.90
Total value
£0.00
Is there a problem with PHP adding two figures together when one is ZERO?
Any help would be great. Thanks
It seems you should change this line:
$total_value = utf8_encode(money('%n', $bawtry_value + $chain_value));
into
$total_value = utf8_encode(money('%n', $bawtry * $price + $chain * $price));
or even
$total_value = utf8_encode(money('%n', $price * ($bawtry + $chain)));
Now in calculating $total_value you have currency symbol at the beginning of $bawtry_value and $chaing_value and if you add them, they are converted to 0.
You can see this if you try to display:
echo '£0.00' + '£39.90';
first character of those strings is £ is converted to 0.
However if you try to do something like that
echo '2£0.00' + '39.90£';
result will be 41.90 because strings are converted to int until the first character that makes the string cannot be converted to int.
I am trying to create a binary/hexadecimal converter to convert a denary(base 10) number/value into binary and hexadecimal.
It works fine so far for binary until the input from the form is greater than 11 digits and over(string length), ruffly as it seems to variety. after 11 digits it starts adding " - " into the outcome. Im not sure were this is coming from as I don't have an " - " in the code.
I wasn't sure if this was something to do with large integers as I saw some other questions on that topic(not in php however it was java, so not sure if there is something simpler in php)
That said I was under the impression that form inputs were always strings.
To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric(). - from : http://www.php.net/manual/en/function.is-float.php
(haven't yet got to hexadecimal but needed to mention it as some of the following code contains parts for it.)
here is the php code (note: I do check user input just not added it yet)
$inpu = $_POST['number'];
$numinput = $_POST['number'];
if (is_numeric($numinput))
{
while ($numinput >= 1)
{
$binary .= $numinput % 2;
$numinput = $numinput / 2;
}
$mult = strlen($binary) % 4;
echo gettype($numinput) . "<br />";
echo gettype($binary) . "<br />";
echo gettype($mult) . "<br />";
echo $mult . "<br />";
while ($mult < 4)
{
$binary.= "0";
$mult++;
}
$revbinary = strrev($binary);
echo $inpu . " in binary = " . $revbinary ;
echo "<br /> <br />";
echo chunk_split($revbinary, 4);
echo "<br />" . gettype($revbinary) . "<br />";
echo gettype($inpu) . "<br />";
}
else
{
if (is_numeric($numinput))
{
echo "$numinput is over the max value of 255";
}
else
{
echo "your entry is not a vaild number <br />";
echo $numinput;
}
}
Im not looking for completed version of this code as you would ruin my fun, I am just wondering why there is a "-" being entered after 11 digits or so. It also did't add the symbol before I added :
$mult = strlen($binary) % 4;
echo $mult . "<br />";
while ($mult < 4)
{
$binary.= "0";
$mult++;
}
This was to split the binary into 4s ( 0011 1101 0010 0110 ).
Edit: wondered if this would be useful:
echo gettype($numinput); result double
echo gettype($binary); result string
echo gettype($mult); result integer
gettype($revbinary); result string
echo gettype($inpu); result string
still trying to work this out myself.
Any advice is much appreciated Thanks
I would suggest simply using decbin() and dechex(). Those are functions included in php, which do exactly what you're trying to accomplish.
You might want to check if it is a number first (like you are already doing).
Then cast it to an integer (through intval()) and then apply decbin() and dechex()
http://php.net/manual/de/function.decbin.php
http://www.php.net/manual/de/function.dechex.php
http://php.net/manual/de/function.intval.php
The following code:
$val = "209810563658809344";
echo sprintf('%0.0f', ($val - 1) + "<br />");
echo sprintf('%0.0f', (bcsub($val, 1)) + "<br />");
In windows 32 bit outputs:
209810563658809344
209810563658809344
How can be made the subtraction to work properly?
Both your sprint() masking and the use of + instead of . for concatenation are forcing a cast to numeric datatype
$val = "209810563658809344";
echo sprintf('%0.0f', ($val - 1)) . '<br />';
echo bcsub($val, 1) . '<br />';
Avoid the conversion to floating point and you'll be fine.
$val = "20981056365880934";
echo bcmul($val, 2, 0)." ≈ ".bcdiv($val, 2, 0)." × 2 + ".bcsub($val, 1, 0)." + 1";
Note the third scale parameter which allows you to control precision like printf(). See the bc manual