How to assign three different value to three the same variables [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I don't know if there is a way out of this...... How can three different values be assigned to the same variable differently and echo or print the variable? Just like the code below.
$A = 'A';
$A = 'B';
$A = 'C';
echo $A;
If I echo $A we all know it going to get the last variable, so how am I going to get all the values once.

You could use an array of values like this:
$A = [];
$A[] = 'A';
$A[] = 'B';
$A[] = 'C';
echo $A[0];
...
echo $A[2];

you have to use array not variable, a variable can hold single value at a time.
there is different way to achieve this
$A = ['A', 'B', 'C'];
print_r($A);
OR
$A[] = 'A';
$A[] = 'B';
$A[] = 'C';
print_r($A);
OUTPUT
Array
(
[0] => A
[1] => B
[2] => C
)
OR
$A = 'A';
$A .= 'B';
$A .= 'C';
echo $A;
OUTPUT
ABC

simple and ugly way
<?php
$A = 'A';
$A .= 'B';
$A .= 'C';
echo $A;
OUTPUT
ABC

Related

How to put multiple parameters in one $ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
Sorry if this is very obvios
I have the following
$a = 1200.00
$b = 675
$c = 123.00
$d = $a$b$c
How would I properly write $d
I thought it would be
$d = '$a'.'$b'.'$c'
How ever this is not correct
How can I make it so when asking to echo out $d
<?php echo $d; ?> it shows:
1200.00675123.00
It's not JS, . will not sum up numbers, but convert it to string
$d = $a . $b . $c;
Please note, that you are working with float numbers, so sometimes instead of 1200.00 you can get 1199.999999999999999999998 and outputting it will trim your .00 part.
That's why you need to use number_format() to output floats in format that you want:
function getFloatStr(float $num) {
return number_format($num, 2, '.', '');
}
$d = getFloatStr($a) . $b . getFloatStr($c);
Example
You are using ', hence the $a are not read as variable, but as a constant string:
$d = $a . $b . $c;
Will concatenate the three variables.
And since you declare $a, $b and $c as number, they are evaluated as such:
<?php
$a = 1200.00;
$b = 675;
$c = 123.00;
$d1 = $a . $b . $c;
$d2 = '$a' . '$b' . '$c';
$d3 = $a + $b + $c;
echo '<pre>';
echo "d1: ", $d1, "\n"; // d1: 1200675123
echo "d2: ", $d2, "\n"; // d2: $a$b$c
echo "d3: ", $d3, "\n"; // d3: 1998
echo '</pre>';
If you want to keep the 00, you will need either to use a formatting function, either to use quote:
printf("printf: %.2f%.0f%.2f\n", $a, $b, $d); // printf: 1200.006750.00
Or:
$a = '1200.00';
$b = '675';
$c = '123.00';

php - different variables with string, one array

Here are my variables:
$a = Hello Mister
$b = I play basketball
$c = You walk in the city
$d = My name is Mary
$e = Where is Bryan
I tried of course the explode " ", but it does not work because each variable has words with spaces.
I also tried to adapt the chunk_split and the str_split, without success.
(...)
You can simply add variables in array separated by comma, see example below:
$arr = array($a, $b, $c);
Or you can also append newly found variable as below :
$arr[] = $d;
The solution is:
Keep the variable names in an array, like this:
$varArray = array('a', 'b', 'c', 'd', 'e');
Create an empty result array, like this:
$resultArray = array();
Loop through this variable array $varArray using foreach loop. In each iteration use variable variables to get the string and append it to the result array $resultArray, like this:
foreach($varArray as $v){
$resultArray[] = $$v;
}
Here's the complete code:
$a = 'Hello Mister';
$b = 'I play basketball';
$c = 'You walk in the city';
$d = 'My name is Mary';
$e = 'Where is Bryan';
$varArray = array('a', 'b', 'c', 'd', 'e');
$resultArray = array();
foreach($varArray as $v){
$resultArray[] = $$v;
}
// display $resultArray
var_dump($resultArray);
You can do that very simply.
$a = "Hello Mister";
$b = "I play basketball";
$c = "You walk in the city";
$d = "My name is Mary";
$e = "Where is Bryan";
$sentence = array($a,$b,$c,$d,$e);
// or index can be assigned manually
$sentence[0] = $a;
$sentence[1] = $b;
$sentence[2] = $c;
$sentence[3] = $d;
// you can add new variable like below. And indexing automatically maintain sequence by itself.
$sentence[] = $e;
echo "<pre>";
print_r($sentence);
echo "</pre>";
// output
Array
(
[0] => Hello Mister
[1] => I play basketball
[2] => You walk in the city
[3] => My name is Mary
[4] => Where is Bryan
)
If you have any further query regarding this you can ask fell free. Thanks

Sorting integer value in php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have 19 variables in a php file.
$a = 20;
$b = 23;
$c = 2;
$d = 92;
$e = 51;
$f = 27;
$g = 20;
$h = 20;
.....
.....
$s = 32;
What i need, I need to show only top 5 value. And there is similar value for some variables. In that case, I need to show the first value only if it is in the top 5 value.
I am not having any clue on doing this.
After receiving some feedback given bellow, i have used array and asort
Here is the example-
<?php
$fruits = array("a" => "32", "b" => "12", "c" => "19", "d" => "18");
asort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>
The output looks like this:
b = 12 d = 18 c = 19 a = 32
I need the reverse result. Meaning, 32, 19, 18, 12.....
Any help. Just dont know the exact command
This is best done by putting the values of the variables into an array and running
sort($arr); (this is from lowes to highest).
rsort($arr); sorts high to low.
http://php.net/manual/en/array.sorting.php
Then you can get the first values at array-index 0,1,2,3 and 4 which will be the biggest numbers.
So:
$arr= array ($a,$b,$c, ....);
rsort($arr);
var_dump($arr); // gives the output.
$arr[0] // biggest number
$arr[4] // 5th biggest number.
A funny way to do this:
$a = 20;
$b = 23;
$c = 2;
$d = 92;
$e = 51;
$f = 27;
$g = 20;
$h = 20;
$array = compact(range('a', 'h'));
rsort($array);
foreach(array_slice($array, 0, 5) as $top) {
echo $top, "\n";
}
Output
92
51
27
23
20
Demo: http://3v4l.org/Wi8q7
Do they need to be individual variables? Storing the values in an array is a better option. So, either manually put all the variables into an array, or change your structure to something more like:
$arr = array(
'a' = 20,
'b' = 23,
'c' = 2,
'd' = 92,
'e' = 51,
....
....
's' => 32
);
or similar. Then use sort() to sort the array:
sort($arr);
To get the top 5, use array_slice():
$arr = array_slice($arr, 0, 5);
See demo
Note: sort() may not be best option for you depending on the desired result. For other sorting options, consult the manual: http://php.net/manual/en/array.sorting.php
<?php
array_push($data,$a);
array_push($data,$b);
.
.
.
$sorted_array = usort($data, 'mysort');
$top5 = array_splice($sorted_array,5);
if(in_array($your_variable,$top5)){
return $top5[0];
}else {
return $top5;
}
function mysort($a,$b){
if ($a == $b) {
return 0;
}
return ($a < $b) ? 1 : -1;
}
?>
$array=array();
for ($i=97;$i<=115;$i++){ //decimal char codes for a-s
$var =chr($i);
$array[]= $$var; //variable variable $a- $s
}
asort($array);
var_dump($array);

How to Adding values of two dimnetional array in php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the below given array
Array
(
[0] => 2,3
[1] => 2,3
)
I want to sum up and get result in a variable. E.g. variable a = 4 (2+2=4) and variable b = 6 (3+3=6). I am coding in php.
Use EXPLODE inside foreach
<?php
$yourArr = array('2,3','2,3');
$a = 0;
$b =0;
foreach ($yourArr as $temp)
{
$tempnew = explode(",",$temp);
$a += $tempnew[0];
$b += $tempnew[1];
}
echo "a = ".$a."<br>";
echo "b = ".$b;
?>
Try this
$a = 0;
$b =0;
foreach ($yourArr as $temp)
{
$a += $temp[0];
$b += $temp[1];
}
echo "a = ".$a."<br>";
echo "b = ".$b;
Output
a = 4
b = 6

one variable changes dynamically depending on previous variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i have this div inside loop
what i want to do is
if the first <div> take $a variable the second <div> want it to take $b variable and so on
so basically the structure will be
<?php
$a = '1';
$b = '2';
while(...){
//// if this div take $a variable
//// the next div will take $b variable
//// and the 3rd div take $a variable and so on
echo "<div id='$a OR $b'>This is a Div</div>";
}
?>
Try this,
you need only one variable that changes dynamically.
<?php
$a = '1';
while(...)
{
echo "<div id='$a'>This is a Div</div>";
if($a=='1') $a='2';
else $a='1'
}
?>
Try this:
$a = '1';
$b = '2';
while(...){
//// if this div take $a variable so the next div will take $b variable and the 3rd div take $a variable and so on
echo "<div id='".rand(1, 2)."'>This is a Div</div>";
}
?>
<?php
$a = '1';
$b = '2';
while(...){
if($c == $a) $c = $b;
else $c = $a;
echo "<div id='".$c."'>This is a Div</div>";
}
?>

Categories