I'm kinda confused in my case,
$counts="$s{$process[$count]}";
if i echo $counts with
echo "$counts";
it still showing the value, how can i echo $counts to show the variable name
$steamroller
(in this case the process is 'team', and count is 'roller')) instead of the value of the variable $system?
EDIT
okay i found it by myself it should be
$counts='$s'.$process."[".$count."]";
thank you..
Just make this:
$s = 's';
$count = 1;
$process[$count] = 'teamroller';
echo $counts = "$$s{$process[$count]}"; // $steamroller
if you echo variable name in "" , php parse that variable an echo its value,
if you want echo variable name with '' like this:
echo '$counts';
Related
I'm facing a problem in this code:
for ($x = 1; $x <= $num; $x++) {
$row8= mysqli_fetch_array($result8);
echo "<td><input id='".$row['StudentID'].'['. $row8['MarksID']."]' name='".$row['StudentID'].'['. $row8['MarksID']."]' type='text'></td>";
$marksID[$x]=$row8['MarksID'];
$count++;
// $ID[$count]=?;
I want to put the id as a variable (<input id='".$row['StudentID'].'['. $row8['MarksID']."]' name='".$row['StudentID'].'['. $row8['MarksID']."]' type='text'>) so that I could use the value of that input in sql query. How could I keep the id in a variable?
The questions is not that clear.
Is this what you are looking for:
http://php.net/manual/en/function.gettype.php
EDIT - After your explanation
I am still not sure if i fully understood. but are looking for something like this.
$ID[$count]= "$row['StudentID'].'['.$row8['MarksID'].']'
so lets say the count = 0 initially and $row['StudentID'] = 1 and $row8['MarksID'] = 2 in the first iteration
so $ID[$count] is $ID[1]
$ID[1] will be equal to 1[2]
$ID[1] = "1[2]"
I create a $values array and then extract the elements into local scope.
$values['status'.$i] = $newStatus[$i];
extract($values);
When I render an html page. I'm using the following
<?php if(${'status'.$i} == 'OUT'){ ?>
but am confused by what the ${ is doing and why $status.$i won't resolve
$status.$i means
take value of $status variable and concatenate it with value of $i variable.
${'status'.$i} means
take value of $i variable, append id to 'status' string and take value of a variable 'status'.$i
Example:
With $i equals '2' and $status equals 'someStatus':
$status.$i evaluated to 'someStatus' . '2', which is 'someStatus2'
${'status'.$i} evaluated to ${'status'.'2'} which is $status2. And if $status2 is defined variable - you will get some value.
I wanted to add to the accepted answer with a suggested alternate way of achieving your goal.
Re-iterating the accepted answer...
Let's assume the following,
$status1 = 'A status';
$status = 'foo';
$i = 1;
$var_name = 'status1';
and then,
echo $status1; // A status
echo $status.$i; // foo1
echo ${'status'.$i}; // A status
echo ${"status$i"}; // A status
echo ${$var_name}; // A status
The string inside the curly brackets is resolved first, effectively resulting in ${'status1'} which is the same as $status1. This is a variable variable.
Read about variable variables - http://php.net/manual/en/language.variables.variable.php
An alternative solution
Multidimensional arrays are probably an easier way to manage your data.
For example, instead of somthing like
$values['status'.$i] = $newStatus[$i];
how about
$values['status'][$i] = $newStatus[$i];
Now we can use the data like,
extract($values);
if($status[$i] == 'OUT'){
// do stuff
}
An alternative solution PLUS
You may even find that you can prepare your status array differently. I'm assuming you're using some sort of loop? If so, these are both equivalent,
for ($i=0; $i<count($newStatus); $i++){
$values['status'][$i] = $newStatus[$i];
}
and,
$values['status'] = $newStatus;
:)
I trying get another variable in php, from existing text but I have issue to do this.
When I echo $address_number; I get list3.
So I need to get variable $list3 and I need echo $list3; after that.
Something like
<?php echo $(echo $address_number;); ?>
But this is not possible. Any solutions?
Solution:
$address_number = $address.$number;
$iframe_url ="$address_number";
<?php echo $$iframe_url; ?>
Thanks everybody!
Use variables variables and the way they work is like so
$adress_number = "list3";
$list3 = "some value";
echo $$address_number;
the above code will output 'some value', what it's doing is using the value of the variable $address_number and treating it as a variable name, so it will look for that variable name and print it's value
I want to echo a value, but hold it as a variable to access that value later.
What's the right syntax for the following?
if (!empty($row['A'])){
$Test = echo '<span>sale</span>';
}
How do i prevent the above PHP block from printing and only print when that variable when is called somewhere like as in.
echo $test;
Is there a way to put function inside echo?
like.
echo 'echo this is a 'if (!empty($row['A'])){
$Test = echo '<span">function</span>';
}''.
I know the above is wrong, please give a reason for your downvote if you are gonna downvote. Thanks.
You don't need to store echo in the variable. When you want to use the variable later, that is when you call echo. Your code should look like:
$test = (!empty($row['A'])) ? 'sale' : '';
This is a ternary operator which is basically a shorthand for the following if/else:
if(!empty($row['A'])) {
$test = 'sale';
} else {
$test = '';
}
In this case, I set it to an empty string if $row[a] is empty so nothing bad happens if you echo it later. (You want to make sure your variable is defined no matter what, so you don't cause an error by trying to call an undefined variable.)
Then, to use it, simply call
echo $test;
Why do you want to put the function inside of an echo? That defeats the point of storing a variable in the first place.
I think what you would want is something like this:
echo "This is a " . returnValue();
function returnValue() {
return "function";
}
The function is now set to return the value "function", in the echo we echo some text and the return value, so what it should echo is: "This is a function"
Assuming you're trying to check if the given variable is empty and then store some text in it, the correct syntax would be:
if (!empty($row['A'])){
$test = '<span>sale</span>';
}
The above code reads: if $row['A'] is not empty, store $test equal to the string given.
Now, you can re-use the variable in your code.
I'm not quite sure what you're trying to accomplish with the second code block, but I assume you're trying to echo the variable value only if it's set. In that case, you can use the isset() function:
echo isset($test) ? $test : '';
This is the shorthand for the following:
if (isset($test)) {
echo $test;
} else {
echo '';
}
First of all, you should do some tutorials and read up on the basics. That said here is what I think you're looking for. For one thing you never do $var = echo 'blah';
First example.
if (!empty($row['A'])){
$Test = '<span>sale</span>';
echo $Test;
}
Then you can use $Test later in the code if you want. To avoid it echoing just remove the echo $test and do it elsewhere.
For your second part using a variable and a ternary is the best option for this
$isItEmpty = empty($row['A']) ? 'empty' : 'not empty';
echo 'This is row is '.$isItEmpty;
You can also to a ternary inline like this, but it's cleaner to use a variable usually.
echo 'This is row is '.(empty($row['A']) ? 'empty' : 'not empty');
This would output: "This row is empty" if it's empty and "this row is not empty" if it's not (both examples would have the same output.
Hope this helps
You don't need to include the echo in the variable.
Where you have
$Test = echo '<span>sale</span>';
Instead have
$Test = '<span>sale</span>';
Then when you want to echo it, simple
echo $Test;
If you would like to echo it according to the if then you can:
if (!empty($row['A'])){
$Test = '<span>sale</span>';
echo $Test;
}
I would like to do something like this:
echo $myObject->value_$id but I don't know proper syntax and I'm not sure if it is possible.
$id is some PHP variable, for example has value 1. In the end, I would like to get $myObject->value_1 but the number part (1) should be dynamic.
The feature is called variable properties:
<?php
$myObject = (object)NULL;
$myObject->value_1 = 'I am value nr 1';
$id = 1;
echo $myObject->{"value_$id"};
This works:
$variableName = 'value_whatever_1337';
echo $myObject->$variableName;
$name = "value_" . $id;
echo $myObject->$name;