Replacing two Variables with each other's Value [duplicate] - php

This question already has answers here:
Is there a PHP function for swapping the values of two variables?
(20 answers)
Swap two variables value without using third variable in php [duplicate]
(2 answers)
Closed 4 years ago.
In this code
$x = 1;
$y = 2;
echo $x.$y; //12
I want to replace $x and $y vlaues with each other, So $x = 2 and $y = 1, Is that possible to be achieved without assigning middle-variables? Something like using a ready-function or like
$x = 1;
$y = 2;
echo $x.$y; //12
$x = &$y;
$y = &$x;
echo $x.$y //22
But instead of 22 I want to get 21.

Related

How to write a variable, which has variable index in php? [duplicate]

This question already has answers here:
Using braces with dynamic variable names in PHP
(9 answers)
Closed 3 months ago.
I have variables like $start1,$start2...$start($no_col). How can I show all the variables with echo in php? in the code below it doesn't work. $no_col can change from 1 to 10. it is not fixed! I want the result show me all $start1,$start2... $start($no_col) values. All the $start1 ..$start10 variable contain date like 2022-12-10;
for ($i=1; $i <=$no_col ; $i++) {
echo $start.${$i};
The result will be like this:
2022-03-10 2022-09-06 ...
You can use get_defined_vars function for get all variables, and after that show only what you need:
<?php
$start1 = 10;
$start2 = 20;
$start3 = 30;
$start4 = 40;
$start5 = 50;
//get all defined vars
$vars = get_defined_vars();
foreach($vars as $var=>$val) {
if (substr($var, 0, 5) == 'start') {
printf('$%s = %s '. PHP_EOL, $var, $val);
}
}
PHPize - online editor

How can I combine variable and strings together and show varable in PHP? [duplicate]

This question already has answers here:
Using braces with dynamic variable names in PHP
(9 answers)
Closed 1 year ago.
I need to combine and show variable but now not show variable;
I need to combine and show variable but now not show variable;
someone can help me fix it
someone can help me fix it
$ab1 = 20;
$ab2 = 30;
$x = 1;
while ($x < 3){
echo '$ab'.$x;
$x++;}
result $ab1 $ab2 / I need to show variable
You can use ${} to create dynamic variable name, so do:
echo ${'ab'.$x};
Try this it should help
$ab1 = 20;
$ab2 = 30;
$x = 1;
while ($x < 3){
$result = ${'ab'.$x};
echo $result . PHP_EOL;
$x++;
}

PHP math divide it weird [duplicate]

This question already has an answer here:
PHP - Get rid of notation
(1 answer)
Closed 8 years ago.
I have
$a = $rowcount + $rowcount2 ; // this is 25
$b = 1000000000;
$total = $a / $b;
this gives me $total = 2.5E-8; but I want 0.000000025. What is causing this?
You need to tell PHP how to format your number
printf('%.9f', $a / $b);
or to store in a variable...
$num = sprintf('%.9f', $a / $b);

PHP output string with variable? [duplicate]

This question already has answers here:
PHP is confused when adding and concatenating
(3 answers)
Closed 8 years ago.
I don't understand why it output -2yyy !
How can I output xxx8yyy ?
$ten = 10;
$two = 2;
echo "xxx".$ten - $two."yyy"; //-2yyy
echo "xxx".($ten - $two)."yyy";
Try this.
Just add brackets:
<?php
$ten = 10;
$two = 2;
echo "xxx".($ten - $two)."yyy"; // xxx8yyy

convert a number to the next 10,20,30 and so on [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php nearest ten-th value
i want a function simillar to ceil but to get the next 10,20,30,40,50,60,70,80,90,100 number
example:
$x = 5;
echo theunction($x);
returns 10 and if $x = 15.5; it should returns 20 and so on
Simply Try
$x = ceil($x/10) * 10;

Categories