PHP output string with variable? [duplicate] - php

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

Related

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

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.

PHP Increase Letter Works, Decrease Does Not [duplicate]

This question already has answers here:
Decrementing alphabetical values
(3 answers)
Closed 7 years ago.
I have the following code.
$letter = 'D';
$letter++;
echo $letter; //outputs E
$letter--;
echo $letter; //outputs E
Why isn't the subtract function working?
You should make use of ord() and chr() functions:
$letter = 'D';
$val = ord($letter);
$val++;
echo chr($val);
$val--;
echo chr($val);

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: best way to assign 10 times the same symbol (space) to a variable [duplicate]

This question already has answers here:
make string of N characters
(5 answers)
Closed 10 years ago.
We have
$symb="_";
$num=10;
We want $ten_symbs to be exactly "__________"; // ten symbols "_".
Whats the fastest and/or the best way to assign ten "_" to $ten_symbs?
str_repeat():
$symbol = '_';
$num = 10;
echo str_repeat($symbol, $num);
You can use str_repeat as:
$ten_symbs = str_repeat($symb, $num);
You can also do:
$ten_symbs = str_pad('',$num,$symb);
But the fist option is cleaner.
look for str_repeat() here: http://www.php.net/manual/de/function.str-repeat.php
$ten_symbs = '';
for($i=0;$i<$num;$i++) {
$ten_symbs .= $symb;
}

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