Is there a way I could possible change raw text to a variable name in a echo. For instance settings a variable $number to number1, and then when it echoes to use $number1?
To clarify, say we have a variable named $number = "number1"; in it, and when we echo $number , it echos a variable named number1, how would I change number1 to a variable? Something like "echo "$"."$number";?
You can try the following
$number = 2012 ;
$name = 'number' ;
echo $$name ; //2012
echo ${$name}; //2012
echo ${"number"} ; //2012
echo "${$name}"; //2012
Quite easily:
$number = 'number1';
$$number = '';
var_dump(get_defined_vars());
or
echo $number1;
You can do this with two dollars instead of one. Note that this is really bad practice, and there's probably a better way to achieve your goal here.
$number = 42
$usenumber = 'number'
echo $$usenumber //echos 42
Related
$date_show1 = "01-".$date2;
$date = date($date)."-01";
$date_char = date($date);
$eee2 = mysqli_query($database->connection,"SELECT * FROM bon_info
WHERE date_day = '$date' AND creditcart != '8' AND creditcart != '6' AND
kassa_id = '$kassa_id'") or die(mysqli_error());
$num1 = mysqli_num_rows($eee2);
I tried the following script with a while loop but how can i also change the names of the variables also like
$date_show1 ..2 ..3 ..4 ..5 ..6 ..7 ..8 ..9 ..10 ..11 ..12
$num1 $num2 $num3......
I have no clue what you ask, apart from the numerating variables. You can use an array for that:
$variable[0] = 123;
$variable[1] = 123;
$variable[2] = 123;
$variable[3] = 123;
for($i=0;$i<=3; $i++){
echo $variable[$i];
}
Better usage for this would be a foreach. Imagine you unset value 2, the $i will echo a blank echo, because it doesn't exist. Waste over effort, therefor:
foreach($variable as $key=>$value){
echo $key.' = '.$value;
}
That method also supports non-digit keys (like ['name'] and ['id'])
You can concat a variable to make a variable name like that:
<?php
$my_date_var = "date_show1";
$my_date_var++; // date_show2
echo $$my_date_var; // if defined will echo variable $date_show2
I am having a basic php problem that I haven't been able to resolve and I would also like to understand WHY!
$upperValueCB = 10;
$passNodeMatrixSource = 'CB';
$topValue= '$upperValue'.$passNodeMatrixSource;
echo $topValue;
OUTPUT
$upperValueCB
but I want OUTPUT as the variable's value 10.
How can I make PHP read the $dollar-phrase as a variable and not a string?
$varName = 'upperValue' . $passNodeMatrixSource;
$topValue = $$varName;
http://php.net/manual/en/language.variables.variable.php
This little example might illustrate what you are about to do:
<?php
$a = 'b';
$b = 10;
echo ${$a}; // will output 10
So you will have to change your example to:
$upperValueCB = 10;
$passNodeMatrixSource = 'CB';
$topValue= 'upperValue'.$passNodeMatrixSource;
echo ${$topValue}; // will output 10
You can do it this way :
$upperValueCB = 10;
$passNodeMatrixSource = 'CB';
$topValue= ${'upperValue'.$passNodeMatrixSource};
echo $topValue;
Because upperValueCB is the var name, if you want PHP to understand which var to use, you have to give the var name.
For that, you can use static way like $upperValueCB
or using a string like this : ${'upperValueCB'}
or using a third var containing the var name $var = 'upperValueCB'; $$var;
$upperValueCB = 10;
$passNodeMatrixSource = 'CB';
$topValue= 'upperValue'.$passNodeMatrixSource;
echo ${$topValue};
I know this is not the proper way to do this, however I am trying to put a quick fix on a form that was done by another developer. Basically I want to add an incremental number to a variable inside a while statement:
$count = 1;
while ($r = mysql_fetch_array($query)) {
$variable . $count = $r['somefield'];
$count++
}
So that makes the variables:
$variable1
$variable2
$variable3
....etc
$varname = 'variable' . $count;
$$varname = $r['somefield'];
http://www.php.net/manual/en/language.variables.variable.php
You'd be better off with an array...
$variable[] = $r['somefield'];
You can use variable variables, however it is probably not a good idea, especially for a trivial case like this one.
Might be an easy question for you guys. can't find it on google.
I am trying to concatenate two variables name;
$i=0;
for ($i=0;$i<5;$i++){
if($array[$i]>0){
$test.$i=//do something
}else{
$test.$i=//do something
}
}
//echo $test0 gives me nothing.
//echo $test1 gives me nothing.
I know I can't use $test.$i but don't know how to do this.Any helps? Thanks!
try ${$test.$i} = value
EDIT: http://php.net/manual/en/language.variables.variable.php
I'm assuming that the variables are called $test0, $test1, ..., $test5. You can use the following:
${"test".$i}
Though, might I suggest that you make $test an array and use $i as the index instead? It's very odd to use $i as an index to loop through a list of variable names.
As an example, instead of:
$test0 = "hello";
$test1 = "world";
Use:
$test[0] = "hello";
$test[1] = "world";
Try this:
for ($i=0;$i<5;$i++){
$the_test = $test.$i;
if($array[$i]>0){
$$the_test=//do something
}
else{
$$the_test=//do something
}
}
This might work:
$varName = $test . $i;
$$varName = ...
Can i ask where for this is neccesary?
Consider my variable $result to be 01212.... Now if i add 1 to my variable i get the answer 1213,but i want it to be 01213.... My php code is
echo sprintf('%02u', ($result+1));
Edit:
Answers to this question works.. But what happens if my variable is $result to be 0121212
in future...
You can use %05u instead on %02u
echo sprintf('%05u', ($result+1));
EDIT:
To generalize it:
<?php
$result = "0121211";
$len = strlen($result+1) + 1;
printf("%0${len}d", ($result+1)); // print 0121212
?>
you could try:
str_pad($result, 5, "0", STR_PAD_LEFT);
Maybe I'm missing something here but it could be as simple as
$result = '0'. ($result+1);
edit:
$test = array('01212', '0121212', '012121212121212', '-01212');
foreach( $test as $result ) {
$result = '0'.($result+1);
echo $result, "\n";
}
prints
01213
0121213
012121212121213
0-1211
( you see, there are limitations ;-) )
Read here: http://php.net/manual/en/function.sprintf.php
in sprintf, you also has to specify length you want - so in your case, if you want any number to be shown 5chars long, you haveto write
echo sprintf ('%05d', $d); // 5 places taking decimal
or even better
printf ('%05d', $d);
Seems like you want to have a '0' in front of the number, so here would be the simplest :P
echo '0'. $result;
Or if you insist on using sprintf:
$newresult = $result + 1;
echo sprintf('%0'.(strlen(strval($newresult))+1).'u', $newresult);