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}";
?>
Related
I am trying to echo the variable $ipss instead of the string below where you see "HERE" in my URL but I don't know how to do it.
<?php $url = "http://api.openweathermap.org/data/2.5/weather?q=HERE&lang=fr&units=metric&appid=b5f11a80423d0d4dae64f1ec1a653edf"; ?>
I have tried this to output the value of the variable in place but it doesn't work:
<?php $url = "http://api.openweathermap.org/data/2.5/weather?q=echo $ipss&lang=fr&units=metric&appid=b5f11a80423d0d4dae64f1ec1a653edf"; ?>
You can concatenate a value into a string using the . concatenate operator.
<?php $url = "http://api.openweathermap.org/data/2.5/weather?q=" . $ipss . "&lang=fr&units=metric&appid=b5f11a80423d0d4dae64f1ec1a653edf"; ?>
You can use {} when the full string is within double quotes, this substitutes the value of the variable in place.
<?php $url = "http://api.openweathermap.org/data/2.5/weather?q={$ipss}&lang=fr&units=metric&appid=b5f11a80423d0d4dae64f1ec1a653edf"; ?>
Im trying to echo some JSON Data. The problem is the data contains variables but my code isn't putting the variables into the string.
Heres my code:
$status = $row['Status'];
$priority = $row['Priority'];
echo '{"status":"$status","priority":"$priority"}' ;
this php is echoing
{"status":"$status","priority":"$priority"}
when I need to echo
{"status":"Completed","priority":"High"}
for example. How can I fix this?
Just use json_encode function
echo json_encode($row);
json_encode($row)
Will give you the desired output.
The problem here is that PHP does not substitute variables in single quotes, only in double quotes (see http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double).
For example:
$test = "a";
echo 'This is $test test and'.chr(10);
echo "this is $test test.".chr(10);
/*
Creates the following output:
This is $test test and
this is a test.
*/
Note: chr(10) creates the new line.
And the solution to your problem is to use json_encode() and json_decode() as other people have suggested already.
http://php.net/manual/en/function.json-encode.php
The problem is in your single quotes, PHP get all vars inside as strings, so break the string as follow:
echo '{"status":"'.$status.'","priority":"'.$priority.'"}' ;
On top of that, you can use json_encode() in order not to build your JSON object manually.
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;
}
How can I do that?
I have something like:
define($stuff.'_FOO', 'whatever');
echo $stuff.'_FOO';
and it doesn't work :(
I just want to echo the constant's value...
Check out constant().
In your case:
echo constant($stuff . '_FOO');
First make a constant:
define("FOO_BAR", "something more");
then you can get the value by using constant():
echo constant("FOO_BAR");
Read more about constants in the manual.