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

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

Related

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 sum last 6 digit from substring [duplicate]

This question already has answers here:
Get the sum of all digits in a numeric string
(13 answers)
Closed 8 months ago.
I'm trying to adding all numbers from last 6 digit from substr(). Let say the number is 19283774616, I'm trying to have result from this: 7+7+4+6+1+6 = ?. Here is my current code
public function accountHash($accountNumber)
{
$result = 0;
$accountNumber = substr($accountNumber, -6);
for($i=0; $i<=strlen($accountNumber); $i++) {
$result += substr($accountNumber, $i, 1); // A non-numeric value encountered here
}
echo $result;
}
From the function above, "A non-numeric value encountered" error occurred. Need suggestion on how to do this. Thank you
You attempt to get more characters than string contains. Replace "<=" with "<" in your condition expression, i.e. change:
for($i=0; $i<=strlen($accountNumber); $i++) {
to
for($i=0; $i<strlen($accountNumber); $i++) {
You need to use < instead of <= in your for loop.
And you can do it a more simple way,
$result = 0;
for($i = 0; $i < 6; $i++){
$result += $string[-$i];
}
An alternative method without loops (or error checking, for what it's worth):
function accountHash($accountNumber)
{
return array_sum(
preg_split('//u', mb_substr($accountNumber, -6), null, PREG_SPLIT_NO_EMPTY)
);
}
Demo

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 5, for loop not displaying correctly series of numbers that starts with 0 [duplicate]

This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 8 years ago.
I'm trying to get the results of the for loop like: 00001, 00002, 00003, etc. but the result is not displaying 0's: instead I get: 1, 2, 3, etc.
This is the code:
$min = 00001;
$max = 00005;
for ($x = $min; $x <= $max; $x++) {
echo "$x ";
}
Just change your echo statement to this:
echo sprintf('%05d', $x);
Also i would recommend you to change your $min and $max variables to this:
$min = 1;
$max = 5;
Because if you have leading zeros then number gets interpreted as an octal number! So 00012 would not be 12 it would be 10.
<?php
$min = 00001;
$max = 00005;
for ($x = $min; $x <= $max; $x++) {
printf("%5d",$x);
}
?>
this is new code
<?php
$min = 00001;
$max = 00005;
for ($x = $min; $x <= $max; $x++) {
printf("%05d",$x);
echo '<br>';
}
?>
Thanks a lot for the help. Doing the trick via MySQL got everything done. Just had to change the value to decimal and assign the field size (7) with the corresponding zerofill attribute.
Now i can insert the count of the for loop without zeros but the db inserts them for me. Thanks

Assign a variable with list() [duplicate]

This question already has answers here:
How can I measure the speed of code written in PHP? [closed]
(10 answers)
Closed 1 year ago.
When we have to return only one value parsed by sscanf / fscanf, should we assign a list of one variable or use optional assigned values?
E.G.
list($number) = fscanf($handle, "%d\n")
or
fscanf($handle, "%d\n", $number)
Is there any difference in execution speed of these expressions?
Just benchmark your two ways with a script like this:
<?php
function micro_time() {
$temp = explode(" ", microtime());
return bcadd($temp[0], $temp[1], 6);
}
$time_start = micro_time();
for ($i=0; $i<100; $i++) {
// the code you want to benchmark
}
$time_stop = micro_time();
$time_overall = bcsub($time_stop, $time_start, 6);
echo "Execution time - $time_overall Seconds";
?>

Categories