PHP String Literal - php

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

Related

PHP include variable within URL?

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

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 can I process an array variable that hide inside another variable?

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

Assigning a value to PHP object property

I'm working through a course on PHP and I'm completely stuck on one of the tasks.
The bit i'm stuck on is:
"PalprimeChecker [a function created just for this task] objects have a property called number. This task has two parts. First, assign that property a value of 17...."
The code I've written is returning an error and won't let me progress...
include('class.palprimechecker.php');
$checker = new PalprimeChecker();
$checker->number = '17';
echo "The number " . "$checker";
echo "(is|is not)";
echo " a palprime.";
I'm not sure at all where I'm going wrong with this. Anyone know the correct way to assign this value?
Hope you can help as I'm tearing my hair out!
Thanks!
Modify this :
echo "The number " . "$checker";
By This :
echo "The number " . $checker->number;
You were trying to print the entire object (which isn't possible without creating a method for it, check orangePill's answer for this), what you wanted to do is simply print the number inside the object.
Also note that you don't need to use quotes when assigning numbers. It might cause issues later on. You should simply assign it like this :
$checker->number = 17;
You can also add a __toString method on the PalprimeChecker class.
public function __toString(){
return (string)$this->number;
}
This will allow echo "The number " . $checker; to produce a string.
Be careful with the concatenation.
<?php
include("class.palprimechecker.php");
$checker = new PalprimeChecker;
$checker->number = 17;
echo "The number" . $checker->number . "";
echo "(is|is not)";
echo " a palprime.";
?>
Well, you are assigning a string, and not a number.
$checker->number = 17; //Note that there are no quotes around '17'.

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

Categories