How can I process an array variable that hide inside another variable? - php

How can i save the $box1 and $box2 in DB and take it out as variable? so that i can loop it.
if($m_name[$x]=='a'){
echo '<input type="checkbox" name="chkbox[][]" (in_array($box1[$i][$j], $autocheck_array) ? ' checked="checked"' : '') . '"/>';
}
elseif($m_name[$x]=='b'){
echo '<input type="checkbox" name="chkbox[][]" (in_array($box2[$i][$j], $autocheck_array) ? ' checked="checked"' : '') . '"/>';
}
$box1 and $box2 are row title in DB,so im not able to loop it

Remove the quotes. Array values don't work simply like that inside quotes
<?php
$abc[1][2]=1;
$a=$abc[1][2];
echo $a;
?>

You need to use {} when interpolating complex variables:
$a = "The value of a[1][2] is {$abc[1][2]}.";
See the section Complex (curly) syntax in the PHP strings documentation.
Note that you only need to do this if you're interpolating as part of a larger string. If you're just trying to assign the array value directly, you don't need to put it in quotes. You can just assign:
$a = $abc[1][2];

Do you mean this ?
$abc[1][2]=1;
$a=$abc[1][2];
echo $a;// Outputs 1

by using this statement $a="$abc[1][2]"; you are actually assigning $a a string value
remove the quotes
<?php
$abc[1][2]=1;
$a=$abc[1][2];
echo $a;
?>

Related

PHP how to echo variable name

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';

How to call double echo or double variable in php

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

How to equate an echo value to a variable and access that variable somewhere else

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;
}

How to use $_GET in a string?

I have a simple PHP file with the following:
<?php
echo 'catid=$_GET["catid"]';
<?>
When I run the page, the output is:
catid=$_GET["catid"]
I'm accessing the page as www.abc.com/temp.php?catid=3. I'd like $_GET to execute so I see:
catid=3
What am I doing wrong?
You have to cancat the two:
echo 'catid=' . $_GET["catid"];
or you could use " (double quotes):
echo "catId=$someVar";
$_Get is a variable, and to echo a variable you do not need parenthesis around it.
<?php
echo 'catid='.$_GET["catid"];
?>
please see this : source
You can use non-array variables for that:
$getCatID = $_GET["catid"];
echo "catid=$getCatID";
Or you can use (recommended):
echo 'catid=' . $_GET["catid"];
You may try:
echo "catid= {$_GET['catid']}";
The best way to use variables in string is:
echo "catid={$_GET['catid']}";
You have to use double quoted string to notify PHP that it might contains variables inside. $_GET is array, so you will need to put the variable statement in {}.
<?php
echo "catid={$_GET['catid']}";
?>
There are a few options to combine a variable with a string.
<?php
$var = "something";
// prints some something
echo 'some ' . $var; // I prefer to go for this one
// prints some something
echo "some $var";
// prints some $var
echo 'some $var';
// prints some something
echo "some {$var}";
?>

PHP String Literal

I am trying to create a string in PHP that looks like this:
<xsl:sort select="TITLE"/>
I have tried a bunch of different ways to create this and echo it out just like that, and have run into errors. So far, I have the variable $myvar hold the value TITLE, and added quotes around it using
$myvar="\"". $myvar . "\""; such that $myvar now looks like this "TITLE"
but trying to concatenate the rest gives me problems. It doesn't seem to like the <. I can echo that out as a variable if I put it in quotes, but if I try to concatenate it to any other string it disappears.
Any ideas how I can get that first string to be stored into a variable using the $myvar variable?
Thanks
$foobar = 'TITLE';
echo '<xsl:sort select="' . $foobar. '" />';
echo '<xsl:sort select="' . $myvar . '"/>';

Categories