PHP include variable within URL? - php

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"; ?>

Related

Echoing JSON Data in php

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.

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 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 . '"/>';

How do I parse URL GET data having the URL stored in a variable?

I have an url stored in a variable such as:
$url = 'example.com?a=20&i=10'
How do I get values stored in variable a and variable i? Is there a short way to do this?
You could use parse_url and parse_str:
<?php
$url = 'example.com?a=20&i=10';
$tmp=parse_url($url);
parse_str($tmp['query'],$out);
print_r($out);
?>
Demo
You can use parse_url().
data = parse_url($url)
print_r($data['query'])
For more details, refer php manual.
Try this:
<?php
$url = 'example.com?a=20&i=10';
$result = parse_url($url);
parse_str($result['query'],$getVar);
echo 'value of a='. $getVar['a'];
echo 'value of i='. $getVar['i'];
If you want to access those variables, check out the extract() function, it will create variables $a and $i from the parse_str() function above.
However, it will overwrite any existing variable called $a so its to be used with caution.
<?php
$url = 'example.com?a=20&i=10';
$tmp=parse_url($url);
parse_str($tmp['query'],$out);
extract($out);
?>

Categories