How to put multiple parameters in one $ [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 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';

Related

How to assign three different value to three the same variables [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 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

Replace occurences of string corresponding to a regex

I have the following strings:
$a = "test1";
$b = "test 2";
$c = "test<3";
$d = "test&4";
I would like to replace occurrences of "&" followed by some letters and terminate by a ";".
The output should be :
$a = "test1";
$b = "test 2";
$c = "test 3";
$d = "test&4";
How can I do that with PHP?
In this particular case, you don't need a regex, most likely what you need is to decode the HTML entities, and that can be done with html_entity_decode(), as in:
$a = html_entity_decode("test1");
$b = html_entity_decode("test 2");
$c = html_entity_decode("test<3");
$d = html_entity_decode("test&4");
var_dump($a,$b,$c,$d);
Use this:
$x = preg_replace('/&[a-z]+;/', ' ', $b);
echo $x;
The answer of #this.lau_ is the best, but if you want the regexp, try this
(\&)([a-z]{1,4})(;)

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 split a string and stored in a php 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 8 years ago.
Improve this question
I will split a long string into 3 parts.how will it can be stored in 3 different variables in php. the code is
<?php
$str = "Q5vRFsC+6Rs08JlvWDMc/sqwR6MpPTzO/p6UH+bPDBE=";
$parts = str_split($str, strlen($str)/3+1);
print_r($parts);
?>
The output is
Array (
[0] => Q5vRFsC+6Rs08Jl
[1] => vWDMc/sqwR6MpPT
[2] => zO/p6UH+bPDBE=
)
pls help me
All you simply need is
$str = "Q5vRFsC+6Rs08JlvWDMc/sqwR6MpPTzO/p6UH+bPDBE=";
list($a,$b,$c) = str_split($str, strlen($str)/3+1);
The variables $a, $b and $c contain your desired values.
Fiddle
This should work for you:
$str = "Q5vRFsC+6Rs08JlvWDMc/sqwR6MpPTzO/p6UH+bPDBE=";
$parts = str_split($str, strlen($str)/3+1);
echo "a = " . $a = $parts[0] . "<br />";
echo "b = " . $b = $parts[1] . "<br />";
echo "c = " . $c = $parts[2] . "<br />";
Output:
a = Q5vRFsC+6Rs08Jl
b = vWDMc/sqwR6MpPT
c = zO/p6UH+bPDBE=
With this code you can output $a, $b, $c and get the single parts of the string
The code is simple,
$string1 = $parts[0];
$string2 = $parts[1];
$string3 = $parts[2];
That is so simple, the point to be noted here is that this site is not for a learning programmer, you need to perform some basic research before posting a question here.
<?php
$str = "Q5vRFsC+6Rs08JlvWDMc/sqwR6MpPTzO/p6UH+bPDBE=";
$parts = str_split($str, ceil(strlen($str)/3));
print_r($parts);
$part1 = $parts[0];
$part2 = $parts[1];
$part3 = $parts[2];
?>

How to make an array of elements which not in range of another array [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
There is an array of numbers from 0 to 10000
$a = array();
$a = range(0,10000);
I have some values which are dynamic coming from database are in array like
$b = array("100-200","400-500","700-900");
so basically i want an array that will look like
array("0-100","200-400","500-700","900-10000");
for example-> if i started a value from 0 so it will break on 100.so i will get 0-100 as first element of an array,then nothing will happen until 200.Again 200 the value will start and go to 400 and will stop then i get 200-400.After that nothing will happen until 500.it will again start with 500 and will stop on 700.so i will get third element as 500-700 and so on...
Anybody can help?
if you want your ranges to be as string element of array, try this:
<?php
$b = array("400-500","700-900","100-200");
asort($b);//new line to sort the ranges
$MIN = 0;
foreach($b as $rang){
$limits = explode('-', $rang);
$result[] = $MIN." - ".$limits[0];
$MIN = $limits[1];
}
$result[] = $MIN." - 10000";
print_r($result);
?>
You can try something like this
<?php
$b = array( "100-200","400-500","700-900" );
$c = array();
$starting = 0;
$ending = 100000;
$last = $starting;
$a = array(); // not being used
$a = range( $starting, $ending ); // not being used
foreach( $b as $k => $v )
{
$values = explode( '-' , $v);
if ( $values[0] > $starting && $values[0] < $ending )
{
$c[] = $last.'-'.$values[0];
$last = $values[1];
if ( $last <= $ending && $k == count( $b ) -1 )
{
$c[] = $last.'-'.$ending;
}
}
}
print_r( $c );
?>
Please bear in mind that I did not use the original $a array for anything. I don't understand it's purpose, unless it's not actually generated from a range, and if so this could should be changed as well

Categories