I'm having hard time with quotation marks. I have this line of code;
echo "<a href='$bName'_read.php?bid='$bid'&id='$next_id[id]'>NEXT</a>";
with 3 variables, $bName,$bid, and $next_id[id].
There is something wrong with the quotations I've used. I also tried this;
echo "<a href='".$bName."_read.php?bid=".$bid."&id=".$next_id['id']."'">";
but it's still not working.
Can anyone explain how quoting works in this case please?
You don't need to put single quotes around every PHP variable. It should make sense in HTML instead, for example;
echo "<a href='{$bName}_read.php?bid={$bid}&id={$next_id['id']}'>NEXT</a>";
You need curly braces ({}) around object and array variables, but it is also useful for normal variables. Also, the array index should be in quotes as it is a string (not required for integer indexes).
Additionally, I changed the ampersand (&) to & as & signifies the start of a special character code (just like &), so although in this case it wouldn't be a problem it is best practice to put the HTML char code in, even in a URL.
Related
So I am trying to link using data I got from a function but it keeps giving me a blank value for ID. Here's my code for what I'm trying to print
<h3 style="text-align: center;">Seller: <?php $sellername =
getNameFromListingID(); $id = getIDByUsername($sellername); echo "".$sellername."";?></h3>
The functions work properly, I have tried printing both of them and it works. They're in a file called getinfo.php, which I have
Include 'getinfo.php';
At the top of my document.
The link with the name works but I always get seller.php?id=, with no value after. Any clue as to why?
You're ending the href attribute too early.
<a href=\"seller.php?id=".$id."\">
This will put the $id inside the href attribute, where it belongs.
Use single quotes in PHP, it's a good practice to get into, and it's also slightly (a teeny tiny bit) faster for PHP to process. Why? Because, when you use double quotes, you're telling PHP that your string contains variables that may need to be evaluated.
So in truth, you don't even need the quotes around variables here.
echo "$sellername";
But doing it like this would be following a best practice.
And now you don't need to escape \" double quotes that HTML uses.
echo ''.$sellername.'';
Caution: It's also a very good idea to escape special characters in anything you're outputting into HTML markup. That avoids the potential for an XSS vulnerability. See: htmlspecialchars()
echo ''.htmlspecialchars($sellername).'';
My question is why do we use the php bracing. Take for example :
let say I want to echo out something like this Now date and time is <23-07-15 00:02:45>
$dateNtimeToday=date('d-m-y H:i:s');
$dateNtime="Now date and time is <{$dateNtimeToday}>";
OR
$dateNtime="Now date and time is <".$dateNtimeToday.">";
OR
$dateNtime="Now date and time is <$dateNtimeToday>";
in any of these cases
echo $dateNtime;
I am just wondering what is the differences and implications
The short answer is this: embedding variables in strings using the "brace" syntax you refer to is usually easier to read and understand than a bunch of concatenation.
Wrapping braces around the variable name helps PHP determine exactly where the variable name starts and stops within the string, so it doesn't get mixed up with literal characters around it.
Here's an example straight from the docs about why this can be an issue:
If a dollar sign ($) is encountered, the parser will greedily take as many tokens as possible to form a valid variable name. Enclose the variable name in curly braces to explicitly specify the end of the name.
<?php
$juice = "apple";
//This will work as expected
echo "He drank some juice made of {$juice}s.";
//Doesn't work due to the trailing 's'
//PHP thinks you're trying to use a variable called $juices, which doesn't exist
echo "He drank some juice made of $juices.";
Read more: http://php.net/manual/en/language.types.string.php#language.types.string.parsing
I have a hint echo'd however, i have a issue with " and ' i can echo numerical values to the string, but not words..
$hint='<a href="javascript:void(0)"
onclick="javascript:document.contactForm.musicDetailTitle4.value=5;
document.contactForm.musicDetailArtist4.value=foo;">fill form</a>'.
5 works but foo doesn't works.
UPDATE
Still not getting an output
$hint='fill form'.
Whole Code
echo $hint='fill form'.$artist."-".$title."-".$id."</a>";
Output is...
fill formTomato Soup-Heinz-0001fill formTomato Soup-Heinz-0001
You need to escape the quotes
$hint='fill form'.
It doesn't have much to do with PHP but rather JavaScript.
When passing a numeric value you just pass the number itself, but when passing strings you must wrap them in quotations otherwise the compiler will mistake "foo" for a variable named foo which may or may not exist.
As others mentioned, all you have to do is wrap your string like so:
\'foo\'
The slashes are because you don't want to close your echo which was also opened using a single quote, so you need to escape the character so when it's echoed to the user it will become 'foo'.
Try this -
$hint='fill form'.
When declaring a string value you must add quotes, and when adding it in this way you must escape those quotes using the \ key.
I want PHP to echo a string stored in a variable, immediately followed by a set of square brackets containing some other text. This goes into a form that will be sent back to PHP, so PHP needs to be able to interpret the output of this command as an array item.
Here are the methods I've tried:
$var='string';
echo "string[0]" //works, but only because the variable isn't used
echo "$var[0]"; //PHP tries to treat the string as an array
echo "$var\[0]"; //the slash gets echoed
echo "$var[0\]"; //syntax error
echo "$var"."[0]"; //this is what I'm using now. It's very ugly and I want an alternative
Is there any way to make this work without breaking the string into chunks and concatenating them?
Here are the top-two ways I can think of to do this in a single output statement, the one you choose will end up being what fits your personal preference the most (and there are probably others available as well):
printf('%s[0]', $var);
echo $var . '[0]';
You can use the curly brace syntax. From the PHP manual documentation on Strings:
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 }. Since { can not be escaped, this syntax will only be recognised when the $ immediately follows the {.
Surround the expression within curly braces (like so: {$var}), so PHP knows where the variable begins and ends.
$var = 'foo';
echo "{$var}[0]"; // => foo[0]
This way, you wouldn't have to worry even if the variable was a quoted array index like $var['foo'] either.
Make it explicit where the variable ends with curly brace syntax:
echo "{$var}[0]";
I'm trying to write a php script that will generate a variety of new php pages, but I'm finding that I'm unable to write a square bracket out. When I escape a square bracket in the same way as other characters (ie [ ) the leading \ is written to the new page, which results in code that doesnt work:
echo $row\['Value'\];
When I do not escape the bracket, the page fails, and the same thing happens when I try and substitute asc(91).
I have seen other examples that use code like $row->Value, but I tried that and it didn't work. If anyone can help me output a square bracket, or knows of another method by which I can fetch a value from a row without using one at all, I'd be very grateful
Your echo would appear as an array reference to PHP. Try this:
echo $row, "['Value'];"
assuming that you want the value of $row to be output, and not the literal text $row. If you want the literal text, (e.g. you're trying to build a PHP script on the fly), then either of these should do the trick:
echo '$row[\'Value\'];';
echo "\$row['Value'];";
How about this:
echo sprintf("\$row['%s']", $value); // either scenario
echo sprintf("%s['Value']", $row);
Keep in mind that PHP automatically parses double quote strings ("), and tries to find variabels within. So, the bracket is probably not the issue, the $ variable prefix (coupled with the parser) probably is.
There are a couple other answers that work but I want to elaborate:
The "echo" construct can take a variable or a string. You can't echo a string to the screen in the same way that you do a variable. For example: echo hello; will not behave as you might think. You need to include it in quotes such as echo "hello";
You can also use single quotes. Single quotes and double quotes behave differently. For example:
$foo = "bar";
echo $foo;
echo "$foo";
echo '$foo';
The first will echo "bar", the second will also echo "bar" because PHP looks for variables in double quotes strings. The third will echo '$foo' because PHP does not try to do variable substitution in a single quoted string. So you can do (as #mark-b said):
echo "\$row['Value']";
or
echo '$row[\'Value\']';
Now, that $row->value syntax that you saw, is object notation. It is assuming that $row is an object and not an array. Objects are a whole other ballgame.
You're talking about code generation in your question, so I expect you also want to output the 'echo' statement in the generated code. Assuming you want to save the output into a file so it can be easily executed, you want to use something like fwrite or file_put_contents, I expect. You need to think in terms of strings, which can be a bit tricky when you're seeing code.
Something like this should work:
fwrite($fp, 'echo $row[\'Value\'];'."\n");
Note how the single and double quotes work. \n is resolved to a newline, but anything in the single quotes is treated as a string and is printed as is, apart from \', which should print a literal single quote in the output file.
Hope this helps.