Simple php .. not working...hmm Looks very simple - php

Please check the code below:
<?php
$d=2;
echo "the sum of the number is"."<sub>($d+1)</sub>";
?>
Its giving as output:
the sum of the number is <sub>(2+1)</sub>
Ideally I need the output to be "the sum of the number is <sub>3</sub>".
It works fine when we don't use the HTML tags <sub>...
How can I fix this?

try writing it as
<?php $d=2; echo "the sum of the number is"."<sub>".($d+1)."</sub>"; ?>
the quotes give it a string representation and thus not allowing you to perform addition.

Move the expression outside the quotes:
<?php
$d = 2;
echo "the sum of the number is <sub>" . ($d+1) . "</sub>";
?>

Related

How to show two Decimal point

I've an online test result page, my code is:
echo ($r1['om']/$r['maxmarks']*100)." %";
Result showing: 61.764705882353 %
How to setup 61.76 % instead?
use number_format()
echo number_format($r1['om']/$r['maxmarks']*100,2)

php session echo intval with decimals

Hello now my code looks like:
echo number_format($_SESSION['price_summ']."<br>");
But I need to have numbers with decimals... I know it should look something like this:
$row['price'] = intval(($row['price']*100))/100;
But it Does not work.
The simplest way is to store your data in database with type of DECIMAL or FLOAT or DOUBLE. And when you will output data from database it will already be in decimal format. Which type to use is relative. Look threw the web to find optimal solution for your situation.
If you want to use PHP use number_format()
$num = "18";
echo number_format((float)$num, 2, '.', ''); //echo will output 105.00
Its really a lot confusing line
echo number_format($_SESSION['price_summ']."<br>");
I guess you want to convert $_SESSION['price_summ'] to float .
Use floatval() for it.
echo number_format(floatval($_SESSION['price_summ']))."<br>";
Alsi you put the bracket after "<br>" which is wrong. In your second code segment you ate simply doing nothing if you $row['price'] is an integer. If it is an integer, go for this
$row['price'] = intval(($row['price']*100.0))/100;

Php set value as a number

How do I output a value as a number in php? I suspect I have a php value but it is outputting as text and not as a number.
Thanks
Here is the code - Updated for David from question below
<?php
if (preg_match('/\-(\d+)\.asp$/', $pagename1, $a))
{
$pageNumber = $a[1];}
else
{ // failed to match number from URL}
}
?>
If I call it in: This code it does not seem to work.
$maxRows_rs_datareviews = 10;
$pageNum_rs_datareviews = $pagename1; <<<<<------ This is where I want to use it.
if (isset($_GET['pageNum_rs_datareviews'])) {
$pageNum_rs_datareviews = $_GET['pageNum_rs_datareviews'];
}
If I make page name a static number like 3 the code works, if I use $pagename1 it does not, this gives me the idea $pagename1 is not seen as a number?
My stupidity!!!! - I used $pagename1 instead of pageNumber
What kind of number? An integer, decimal, float, something else?
Probably the easiest method is to use printf(), eg
printf('The number %d is an integer', $number);
printf('The number %0.2f has two decimal places', $number);
This might be blindingly obvious but it looks like you want to use
$pageNum_rs_datareviews = $pageNumber;
and not
$pageNum_rs_datareviews = $pagename1;
echo (int)$number; // integer 123
echo (float)$number; // float 123.45
would be the easiest
I prefer to use number_format:
echo number_format(56.30124355436,2).'%'; // 56.30%
echo number_format(56.30124355436,0).'%'; // 56%
$num = 5;
echo $num;
Any output is text, since it's output. It doesn't matter what the type of what you're outputting is, since the human eye will see it as text. It's how you actually treat is in the code is what matters.
Converting (casting) a string to a number is different. You can do stuff like:
$num = (int) $string;
$num = intval($string);
Googling php string to number should give you a beautiful array of choices.
Edit: To scrape a number from something, you can use preg_match('/\d+/', $string, $number). $number will now contain all numbers in $string.

How to handle number_format output in php?

I am getting the following output correctly:
<?php echo number_format("12312.312","1"); // Correct Output 12312.3 ?>
but in the following case
<?php echo number_format("12312","1"); // Getting output 12312.0 but requires only 12312 ?>
So basically, I want to control my output i.e. it should add decimal place only if my decimal digit is greater than 0.
The second parameter for number_format() takes the number of decimals - so your example is the the expected result. Maybe you are intereseted in the round() function which allows to round to a certain precision?
You can try something like this
<?php
$number = 12312;
echo is_int($number) ? $number : number_format($number,"1");
?>
If you don't want the extra decimal place, use <?php echo number_format(12312, 0);?>
The "0" represents 0 decimal places
simple casting it to float, usign (float), works.
e.g.
$num_1 = (float)1.0;
$num_2 = (float)1.1;
echo $num_1;
echo $num_2;
output:
1
1.1

PHP calculation

I'm trying to output a value from xml this is what I do -
<?php
echo $responseTemp->Items->Item->CustomerReviews->AverageRating;
?>
This outputs 4.5, but when I change it to the code below it displays as 8. Why isn't it displaying as 9? Thanks.
<?php
echo $responseTemp->Items->Item->CustomerReviews->AverageRating*2;
?>
Try casting the value to a numerical value first.
$num = (double) $responseTemp->Items->Item->CustomerReviews->AverageRating;
echo $num * 2;
See Type Juggling and String Conversion to Numbers on the PHP website for more information.
If you are looking for a decimal value without doing typecasting, you have to multiply by a number with a decimal. Otherwise it will return a regular integer like the number you gave it.
Try multiplying by 2.0
echo $responseTemp->Items->Item->CustomerReviews->AverageRating*2.0;

Categories