variable changes when using if statement [duplicate] - php

This question already has answers here:
The 3 different equals
(5 answers)
Closed 5 years ago.
I've just started learning php an hour ago. I've made this code:
$x=2;
$y=4;
echo $x;
echo $y;
if($x=5)
{
echo "$x";
}
else
{
echo "test";
}
I'm expecting the output: 24test
I'm getting the output: 245
x equals 2 in the beginning. Why is x then changing to 5 when the only thing I do is CHECKING whether x = 5?
I've searched the web and this site for an answer but couldn't find anything. Thanks in advance!
Tony

Note that = is use to assign value while == use to evaluate value within if statement so your code should be
if($x==5) {
Instead of
if($x=5) {

Related

Question about calculating Average in PHP [duplicate]

This question already has answers here:
How to calculate correctly in php?
(6 answers)
Closed 1 year ago.
I am a newbie learning PHP and i am trying to find average of 3 numbers but not getting the correct answer. I don't know where i am going wrong.
function percentage($math,$eng,$sc){
$s = $math+$eng+$sc / 3 ;
return $s;
}
$p = percentage(10,20,30);
echo $p;
I am getting the ansewer as 40 whereas i am supposed to get 20. Kindly check if there is any error.
Return value is right. Check operators precedence.
If you want 20 as return value code is:
$s = ($math+$eng+$sc) / 3 ;
You forgot to use parentheses:
$s = ($math+$eng+$sc) / 3 ;
All things together:
function percentage($math,$eng,$sc){
$s = ($math+$eng+$sc) / 3 ;
return $s;
}
echo percentage(10,20,30);

reverse chunk split of integer in PHP [duplicate]

This question already has answers here:
Formatting numbers with commas PHP [duplicate]
(4 answers)
Closed 7 months ago.
I need split by gap like when we write count numbers we will give gap for every 3 integer.
echo chunk_split(1000255869,3," ");
I got output like: 100 025 586 9
But I need output like: 1,000,255,869
How to reverse that one?
Try this,
<?php
$a = 1000255869;
echo $a;//output - 1000255869
$a = number_format($a);
echo $a;//output - 1,000,255,869
?>
if you want to do a number format, you should do it with number_format.
echo number_format(1000255869,0,".",",");
This should work number_format(1000255869, 0, '.', ',');
Try this.Reference
$number= 1000255869;
echo number_format($number);

How to get not so many digits in PHP [duplicate]

This question already has answers here:
How do I truncate a decimal in PHP?
(6 answers)
Closed 10 months ago.
<?php echo (($priceInt[0]-$specialInt[0])/$priceInt[0])*100 ?>
For every price this gives me something like 11,111111111 or 3,3433333333
How can i limit it so i will get only 11 or 3 ? Without anything after " , ".
Take a look at http://php.net/manual/fr/function.number-format.php
Use it like this :
echo number_format((($priceInt[0]-$specialInt[0])/$priceInt[0])*100, 0, '', '');
If you just need the integer part just use :
echo intval((($priceInt[0]-$specialInt[0])/$priceInt[0])*100);
You can use sprintf() like below:
echo sprintf("%d",'3,3433333333'); //output : 3
or
echo sprintf("%d",'11,111111111'); //output : 11
In your case:
echo sprintf("%d",((($priceInt[0]-$specialInt[0])/$priceInt[0])*100));
Thanks a lot guys i fixed it
echo sprintf("%d",((($priceInt-$specialInt)/$priceInt)*100));

How to check variable have a digit at the end or not [duplicate]

This question already has answers here:
startsWith() and endsWith() functions in PHP
(34 answers)
Closed 8 years ago.
I have 2 variable
1 my file name
and
1 my file name 12
.I want to check wheter there have a digit at the end or not.
Try this...
$test="1 my file name 12";
$te = preg_match_all("/.*?(\d+)$/", $test, $digit);
if($te>0) {
echo $test." have following digit in end".$digit[count($digit)-1][0];
}
Result: 1 my file name 12 have following digit in end 12
See the simple regex below:
$var = 'my file name 2';
if (preg_match('~\d$~', $var)) {
echo 'second filename ends with a digit';
}

Print numbers as words? [duplicate]

This question already has answers here:
PHP: Express Number in Words [duplicate]
(4 answers)
Closed 8 years ago.
Hi I was wondering how can I print out numbers in their word form ? When I did a Google search it showed other peoples script on how to print 234 as two hundred thirty four .
I need 234 as two three four.
Thank you guys!
I need 234 as two three four.
It would be as simple as creating an array of your number to word map, then taking your number and printing out the corresponding value:
<?php
$num_word = array();
$num_word[0] = 'zero';
$num_word[1] = 'one';
$num_word[2] = 'two'
...
$num_word[9] = 'nine';
$num = 234;
foreach(str_split($num) as $w) {
echo $num_word[$w];
}
?>

Categories