<?php
$total=3;
echo '
<div class="idsdiv">.$total.<div> ';
?>
i want to appear $total variable number in the link.why is this script not working?
Enclose the whole string with double quotes to embed variables inside:
echo "<div class=\"idsdiv\">$total<div>";
You need to use double quotes around your HTML and single quotes around your attributes or do this...
echo '<div class="idsdiv">' . $total . '<div> ';
PHP doesn't process variable names in strings enclosed in single quotes.
<?php
$total=3;
echo '<div class="idsdiv">',$total,'<div>';
?>
Look at the string section of php.net (http://php.net/string) they talk about how to use each of the types. One of quote being the ' where nothing is parsed.
<?php $total=3;
echo "<div class=\"idsdiv\">$total<div> ";
?>
you had errors with your quotes
You can print HTML without printing it, like so:
<?php
$total = 3;
?>
<div class="idsdiv"><?php echo $total; ?></div>
When I still did PHP, I found it much easier to manage than escaping tons of quotes and things like that.
You can even do it inside of an if block too:
<?php
if ($foo == 'bar') {
?>
<div>Foo is bar</div>
<?php
}
?>
The method I like is
<?php
$total=3;
echo "<div class='idsdiv'><a href='profile.php?id={$total}'>{$total}</a><div>";
?>
It is my method, but there are plenty of ways to do it. Maybe even too many. If you want more information you can always refer to the documentation.
<?php
$total=3;
echo '<div class="idsdiv">'.$total.'<div>';
?>
You're quote are missing before and after the $total
Related
The following line of code is supposed to echo the current season and a data in a php echo:
<?php echo "<h5>" $_SESSION['username'] '<span class="chat_date">'Dec 25"</span></h5>" ?>
Is the session call true? And how can I fix this code?
if you want echo a string you should use concatenation operator ('.')
<?php echo '<h5>' .$_SESSION['username']. '<span class="chat_date">Dec 25</span></h5>' ?>
or use double-quotes (") to passing data
$usename=$_SESSION['username'];
<?php echo '<h5>"$usename"<span class="chat_date">Dec 25</span></h5>' ?>
There is no need to concatenate a string before echoing it in PHP.
The parameters can be passed individually to echo as multiple arguments for a slight performance (speed) increase. In other words, you can use , instead of ..
For example:
<?php echo '<h5>' , $_SESSION['username'] , '<span class="chat_date">Dec 25</span</h5>'; ?>
I would recommend reading the Official Documentation.
<?php
echo "<h5> $_SESSION[username] <span class=\"chat_date\">Dec 25</span></h5>";
?>
Variables are evaluated inside double quoted strings.
To echo double quotes inside double quotes, you add a backslash before the double quote so PHP knows it is not the end of the quoted segment.
For arrays, do not quote the key inside a string.
This question already has answers here:
php - insert a variable in an echo string
(10 answers)
Closed 3 years ago.
All I want to do is echo out a PHP variable inside a <li> tag. Refer to the code below. What's the proper format? Mine is not working.
$row['cat_title'];
echo "<li> {$cat_title} </li>";
If you need to output a value of a particular array key (e.g. $row['cat_title']) in HTML, you have quite a few options.
You can use the concatenation operator (.):
echo '<li>' . $row['cat_title'] . '</li>';
You can use variable interpolation with simple string parsing (note that in this syntax, quotes around the array key must be omitted):
echo "<li>$row[cat_title]</li>";
You can use variable interpolation with complex string parsing (using {}) which is actually not necessary here, but useful for more interpolating more complex expressions. With this syntax, quotes around the array key should be included.:
echo "<li>{$row['cat_title']}</li>";
You can output plain HTML and use the echo shortcut syntax <?= to output the value (only do this if you are already outputting HTML, not if you are currently in a <?php tag; that would be a syntax error.):
<li><?= $row['cat_title'] ?></li>
You can use printf (thanks to Elias Van Ootegem's comment for reminding me of this; I should have included it to begin with). sprintf can be used if you want to save the result to a variable instead; printf will output it immediately:
printf('<li>%s</li>', $row['cat_title']);
The first argument of printf is a format string, where %s is a string conversion specification that will take the value of $row['cat_title'] when printf is executed.
There are other ways, but these are the most common.
You need to reference the right variable. You're on the right track, though.
echo "<li> {$row['cat_title']} </li>";
echo '<li>'.$var.'</li>';
OR
echo "<li>$var</li>";
OR
<li><?=$var?></li>
If the file is mostly PHP in the file then:
<?php echo "<li>" . $row['cat_title'] . "</li>"; ?>
Or conversely if it's mostly HTML in the file then:
<li><?php echo $row['cat_title']; ?></li>
It sounds to me that it's most likely the second option you are looking for.
Another alternative:
<li><?php echo $row['cat_title']; ?></li>
Either of these methods will work...
// Use single quotes and periods like this...
echo '<li>'. $row['cat_title'] .'</li>';
OR
// Use double quotes like this...
echo "<li> $row['cat_title'] </li>";
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get useful error messages in PHP?
Ive started on part of my new year resolution and decided to learn php, as part of it im trying to parse in an xml feed, and echo out the name of the events wrapped in <a> tags linking them back to the events page on the xml feed's site.
I think ive got it all in but i cant seem to see why this isnt working im just getting a blank page, if some one could point me in the right direction it would be much appreciated, cheers
<?php
// F1 W/H xml feed
$xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N');
foreach ($xml->response->williamhill->class->type as $type) {
$type_attrib = $type->attributes();
echo "<h2>".$type_attrib['name']."</h2>"; //Title - in this case f1 championship
} ?>
<ul>
<?php
foreach($type->market as $event) {
echo "<li>";
echo "<a href="$event_attributes['url']">";
echo $event_attributes['name'];
echo "</a>";
echo "</li>";
}
?>
</ul>
echo "<a href="$event_attributes['url']">";
try changing that line to
echo "<a href=\"".$event_attributes['url']."\">";
The Php parser is pretty funny about this. Usually you pick one and just stick to it, or use both single quotes and double quotes as you please. Just remember that strings with double quotes are parsed for variables.
$hello = "Hello";
echo "$hello master";
is the same as
$hello ="Hello";
echo $hello.' master';
When you are testing your PHP scripts, you'll find it useful to switch on errors - then PHP will actually tell you why it isn't showing you anything:
error_reporting(E_ALL);
Normally you will have missed a ; or mis-typed a variable name.
in your case the error is here:
echo "<a href="$event_attributes['url']">";
You have accidentally ended the string with a double quote, so PHP thinks the string ends here:
echo "<a href="
This is where using single-quotes can be very handy because your double quotes won't then close the string.
echo '<a href="' . $event_attributes['url'] . '">';
The main difference between single and double quotes in PHP is that double quotes has special clever parsing rules and single quotes doesn't. For example:
$myVar = "BLAH";
echo "Example $myVar"; // Example BLAH
echo 'Example $myVar'; // Example $myVar
In your unordered list, you should use a dot to concatenate your string, and escape your double quotes like this:
echo "<a href=\"".$event_attributes['url']."\">";
Instead of
echo "<a href="$event_attributes['url']">";
Your example throws and error because you haven't used proper string concatenation. However, even with correct concat, it would render as <a href=http://someurl>, and you'd need to add the double quotes according to html standard. Hence you have to double quote.
if you want to not be troubled by having to switch between using a ' or a " then i suggest using the php alternative syntax php alternative syntax
with the given code it would look like
<?php
// F1 W/H xml feed
$xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N');
foreach ($xml->response->williamhill->class->type as $type) {
$type_attrib = $type->attributes();
echo "<h2>".$type_attrib['name']."</h2>"; //Title - in this case f1 championship
} ?>
<ul>
<?php foreach($type->market as $event):?>
<li>
<a href="<?php echo $event_attributes['url']; ?>">
<?php echo $event_attributes['name']; ?>
</a>
</li>
<? endforeach;?>
</ul>
one advantage this would bring is that it would produce cleaner code since you can clearly distiguish your php code from your html which is the presentational part at the price writing all those other <?php ?> and as what others would claim a performance degradation. the choice is yours
Change
echo "<a href="$event_attributes['url']">";
for
echo "<a href=".$event_attributes['url'].">";
You are missing the periods in your second echo, where you have your $event_attributes['url']
<?php
foreach($type->market as $event) {
echo "<li>";
echo "<a href=".$event_attributes['url'].">";
echo $event_attributes['name'];
echo "</a>";
echo "</li>";
}
?>
I would recommend you to enable your error log, it would allow you to know the line with problems in any of your scripts.
I'm having trouble escaping the PHP variable inside the getItems function:
while($row = mysql_fetch_array( $data ))
{
echo "<div class='favorite'>";
echo "<div style='display: inline;'>".$row['Item']."</div>";
if ($row['UID'] = $uid) {
echo "<div id='unlock'>Info</div>";
} else {
echo "<div id='unlock' onclick='getItems('".$row['Item']."')'>Unlock</div>";
}
echo "</div>";
}
When rendered (is render the word?) anyway, when I see it on my site it says:
onclick="getItems(" whatever')'
What am I doing wrong?
You can see the code here:
http://www.chusmix.com/game/insert/get-items.php?user=19
Your problem is that your attribute values are surrounded by single quotes, but you're also using single quotes in your javascript.
You'll have to use double quotes in your javascript. However, since the whole string (in PHP) is surrounded by double quotes, you'll have to escape them. Hence:
echo "<div id='unlock' onclick='getItems(\"".$row['Item']."\")' style='display: inline; float: right;'>Unlock</div>";
Or like this:
echo "<div id='unlock' onclick='getItems(\"{$row['Item']}\")' style='display: inline; float: right;'>Unlock</div>";
To clarify what the curly braces do (from the PHP docs):
Complex (curly) syntax
This isn't called complex because the syntax is complex, but because
it allows for the use of complex expressions.
Any scalar variable, array element or object property with a string
representation can be included via this syntax. Simply write the
expression the same way as it would appear outside the string, and
then wrap it in { and }.
To further explain, let's say we have the following scenario:
$name = 'Apple';
$sentence = "$names are my favorite fruit";
What I'm trying to get is: Apples are my favorite fruit. However, this won't work. PHP will instead be looking for a variable called $names, and when it doesn't find it, it'll complain.
So, to remedy this, we can surround our variable in curly braces:
$name = 'Apple';
$sentence = "{$name}s are my favorite fruit";
Great! Now PHP will know where the variable name ends and the string starts.
On a side note: You might consider switching to double-quoting your attributes, since the way you do it now is not valid xHTML (unless you don't care).
Yes, there is a problem with your quotes. It should be this:
echo "<div id='unlock' onclick='getItems(\"".$row['Item']."\");' style='display: inline; float: right;'>Unlock</div>";
The problem is that your opening quotes for onclick and the quotes around the function arguement have to be a different kind of quote.
This is much easier though to do with html and then just insert the variable like this:
<div id="unlock" onclick="getItems('<?=$row['Item'];?>');" style="display: inline; float: right;">Unlock</div>
Doing things this way instead of echoing HTML when possible will save you tons of time and confusion, and you won't have to worry about all the escaping of quotes
The ' inside onclick is closing the onclick itself. Change it to:
onclick='getItems(\"".$row['Item']."\")'
That way, in JS, it uses a different type of quote.
Even better... you can leave PHP, and have one less type of quote to worry about.
else { ?>
<div id='unlock' onclick='getItems("<?=$row['Item'];?>")' style='display: inline; float: right;'>Unlock</div>
<?php
}
or like so:
echo '<div id="unlock" onclick="getItems('."'".$row['Item']."'".')" style="display: inline; float: right;">Unlock</div>';
If I had to do this, it would have looked like:
<?php while(true) :?>
<div class="favorite">
<div style="display: inline;"><?php echo $row['Item'];?></div>
<?php if ($row['UID'] = $uid):?>
<div id="unlock">Info</div>
<?php else: ?>
<div id="unlock" onclick="getItems('<?php echo $row['Item']; ?>)">Unlock</div>
<?php endif;?>
</div>
<?php endwhile;?>
try the following .
edit: changed to make sure quotes were escaped correctly
echo "<div id='unlock' onclick=\"getItems('{$nameArray[0]}')\" ></div>";
There is a problem in this code I can not detected
<?php echo "<a href ='$rows['Link']'> .$rows['UploadName']</a> "; ?>
Do you find you have a solution???
Thank you very much.
My guess is that your problem is that it isn't writing out the data in $rows['Link'] ... if that is the case, then your solution is to change it to {$rows['Link']} ... actually, you'll probably want to change both, since it looks like you started doing string concatenation and then switched halfway through.
So:
<?php echo "<a href ='$rows['Link']'> .$rows['UploadName']</a> "; ?>
becomes:
<?php echo "<a href ='{$rows['Link']}'>{$rows['UploadName']}</a> "; ?>
See: The PHP Manual on Variable Parsing in strings
It should be:
<?php echo "<a href ='{$rows['Link']}'>{$rows['UploadName']}</a>"; ?>
Or:
<?php echo "<a href ='{$rows['Link']}'>" . $rows['UploadName'] . "</a>"; ?>
There's a problem in parsing variables in the string. Use curl braces:
<?php echo "<a href ='{$rows['Link']}'> .{$rows['UploadName']}</a> "; ?>
Take a look to this php.net page, under "variable parsing".
More alternatives:
<?php echo '' . $rows['UploadName'] . ''; ?>
or
<?=('' . $rows['UploadName'] . '')?>
Another alternative (that I tend to prefer, given I know that both 'Link' and 'UploadName' are valid indices of $row.
<?=$rows['UploadName']?>
I'm not sure what that does for readability for most people, but on color-coded IDEs, it tends to help, because the HTML isn't just seen as one giant ugly single-colored string.