How to convert a string in a number PHP - php

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.

Related

formatting strings (variable+text)

I need to add 2 numbers together.
$num1 = 40;
$num2 = 20;
$num12 = $num1 + ':' + $num2;
For some reason this outputs as 60 and it should be 40:20.
is there some way to format this?
yes there is. use php concatenation (.) not javascript concatenation (+).
$num12 = $num1 . ':' . $num2;
What you are looking for is string concatenation.
In php you need the .-operator , not the +:
$num12 = $num1 . ':' . $num2;

How a string of text every other two lines in a foreach loop in PHP? (retrieving records from a database)

I am working on a "boxing records" database for a school project. The loop retrieves records from a SQL statement. I want to add a "VS" string of text between every other two lines in order to show records outputted somewhat like this.
Upcoming Fights
Sergey Kovalev (28-0-25)
VS
Jean Pascal (30-3-17)
Another Boxer (123-0-5)
VS
Some Boxer (123-3-1)
However, my current loop outputs like this
Sergey Kovalev (28-0-25)
VS
Jean Pascal (30-3-17)
VS
Another Boxer (123-3-1)
VS
Some Other Boxer (123-3-1)
VS
The loop I currently have is the following
foreach($records as $record) {
$i = 0;
echo $record['name'] . " (" . $record['wins'] . "-" . $record['losses'] . "-" . $record['kos'] . ")" . "<br>";
$i=$i*2;
if($i%2 == 0)
{
echo "VS <br/>";
}
else{
echo "<br />";
}
I know I could probably change the SQL in order to display two fighters in the same row, and then append "vs" on the echo, but I thought that just modifying the for loop would work by using a variable counter $i. I thought it would be pretty easy to make the "VS" appear between every two rows but im missing something in my logic.
You need to increment value of $i by one rather than multiplying it by 2 and initialize $i outside foreach loop.
$i = 0; // Initialize counter here
foreach($records as $record) {
echo $record['name'] . " (" . $record['wins'] . "-" . $record['losses'] . "-" . $record['kos'] . ")" . "<br>";
if($i%2 == 0)
{
echo "VS <br/>";
}
else
{
echo "<br />";
}
$i++; // Increment counter here
}

unexpected result in php output with larger strings

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

changing font makes + combine strings instead of adding values

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>";

How to round mysql column in a table output

I need to round a result of a SQL round in a table array output. I can't figure out the syntax...
$result = mysql_query("SELECT `Energ_Kcal`*`yield`*`qty` AS `cal` FROM allinnot a
WHERE `own_id` = $user->id");
echo "<tr><td>" . $row['Shrt_Desc'] . "</td><td> " . $row['desc'] . "</td><td>" . $row['cal'] . " cal</td></tr>";
cal returns a value with many numerals beyond the decimal. I just need it to show a whole rounded integer. I tried ROUND(), but I must be putting it in the wrong place.
general syntax
ROUND( expression, 2 )
The ROUND should go around the values, try this:
$result = mysql_query("SELECT ROUND(`Energ_Kcal`*`yield`*`qty`,2) AS `cal` FROM allinnot a
WHERE `own_id` = $user->id");
echo "<tr><td>" . $row['Shrt_Desc'] . "</td><td> " . $row['desc'] . "</td><td>" . $row['cal'] . " cal</td></tr>";
You could also check out PHP's round() or, possibly from your description int_val().

Categories