Another question for all you pros out here. I am still trying to learn the language and don't want to pick up any bad habits along the way, and it is easier to fix a problem now rather than later when it is a habit.
This is regarding the correct way to use the \n in a string.
They way the book I am reading does it is as follows:
print "$student: $grade<br />\n";
After some reading, I have heard that it is best practice to use single quotes whenever possible, and this is how I have written the same statement:
print $student . ': ' . $grade . '<br />' . "\n";
What is the preferred method of doing this? Is the books way or my way more correct?
It seems like the books way is easier to read when reviewing the code, so should I use double or single quotes in a situation like this?
Thank you,
Alex
http://us3.php.net/manual/en/reserved.constants.php
Scroll down to PHP_EOL.
EOL refers to 'end of line'.
Related
I'm trying to redirect to another php page from sql server fetch array with WHILE loop.
All records are fetched well, but i want to add to the table "delete" link to every fetched record.
I tried the code below separatedly and it works just fine.
The problem is because the code is inside PHP tag and i'm confused with all of the ' ' signs.
while compailing the code below i just can't see nothing (without the "delete" line everything is OK):
while ( $record = sqlsrv_fetch_array($rs, SQLSRV_FETCH_NUMERIC) )
{
$o .= '<tr><td><center>'.$record [0].'</center></td><td><center>'.$record [1].'</center></td>
<td><center>'.$record [2].'</center></td><td><center>'.$record [3].'</center></td>
<td><center>'.$record [4].'</center></td><td><center>'.$record [5].'</center></td>
<td><center>'.$record [6].'</center></td><td><center>'.$record [7].'</center></td>
<td>**<a href="JavaScript:if(confirm('delete record?')==true)
{window.location='phpSQLServerDeleteRecord.php?EventID=<?php echo $record[0];';}">delete</a>**
</tr>';
}
$o .= '</tbody></table>';
echo $o;
Thanks a lot for your help!
The use of single-quotes is creating a syntax error:
'</center></td><td>**<a href="JavaScript:if(confirm('delete record?')==true)
{window.location='phpSQLServerDeleteRecord.php?EventID=<?php echo $record[0];';}">delete</a>**
</tr>'
Since the server-side string literal began with a single quote, as soon as the string contains a single-quote then the PHP engine will interpret that as the end of the string, which then results in a couple of syntax errors here as things intended to be part of the string are interpreted as code.
There are a couple of ways to go about this. One quick fix would be to "escape" the single-quotes which are meant to be part of the string rather than a boundary of the string:
'</center></td><td>**<a href="JavaScript:if(confirm(\'delete record?\')==true)
{window.location=\'phpSQLServerDeleteRecord.php?EventID=<?php echo $record[0];\';}">delete</a>**
</tr>'
Of course, there's still the error that now PHP code (the echo statement) is being emitted as part of a string. You can terminate the string boundaries around that and concatenate the value to the string like any other string concatenation:
'</center></td><td>**<a href="JavaScript:if(confirm(\'delete record?\')==true)
{window.location=\'phpSQLServerDeleteRecord.php?EventID=' . $record[0] . '\';}">delete</a>**
</tr>'
(Note that immediately after the reference to $record[0] there are two single-quotes. One to begin the new string literal and then an escaped one to be the first character in that string literal, since the client-side code will need that single-quote character to terminate its string literal. That's probably the root of your confusion in the matter... These server-side strings aren't just HTML, they're also JavaScript which has its own client-side strings.)
You'll also want to URL-encode that value being emitted as part of a query string (even though it should just be a numeric identifier I suspect, it's still good form):
'</center></td><td>**<a href="JavaScript:if(confirm(\'delete record?\')==true)
{window.location=\'phpSQLServerDeleteRecord.php?EventID=' . urlencode($record[0]) . '\';}">delete</a>**
</tr>'
There are a number of other ways to potentially clean this up. Take a look at the PHP documentation on strings, particularly around the "heredoc" syntax. My PHP is to rusty to whip up an example of that, but I suspect it will end up looking a little cleaner in the resulting code. Either way, it's good to get some practice in the differences between the various ways PHP handle string literals.
I will echo the comment from #mario above but give you an example of how to use Heredoc syntax to make large blocs of text much more readable and easy to work with, as you will not need to mess with escaping quotes at all.
while (...)
{
$o .= <<<EOT
<tr>
<td><center>{$record[0]}</center></td>
<td><center>{$record[1]}</center></td>
<td><center>{$record[2]}</center></td>
<td><center>{$record[3]}</center></td>
<td><center>{$record[4]}</center></td>
<td><center>{$record[5]}</center></td>
<td><center>{$record[6]}</center></td>
<td><center>{$record[7]}</center></td>
<td>**<a href="JavaScript:if(confirm('delete record?')==true)
{window.location='phpSQLServerDeleteRecord.php?EventID={$record[0]}}">delete</a>**</td>
</tr>
EOT
}
Now doesn't that look a whole lot nicer?
Note: brackets around PHP variables are optional. I just like them when using Heredoc as I find it makes it easier to see where your variables are.
Read here for more information on Herdoc syntax:
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
For future readers, please look to answer by #David as well. He does good job of explaining why the OP was having his problem. I really look at this answer as complementary to that one, giving the reader an example of what might be a good way to go about solving the problem.
I have a database built and working for my music collection. I have built an entry page that only I have access to. Obviously when I have a title or artist with an apostrophe the entry fails. I've been reading and I found this syntax on a page on this site. However when I try to execute it the 'safe' stringer comes out blank.
This is the part of the code that doesn't work. I'm sure it's something stupid I've done. If anyone can point out the error of my ways I'd greatly appreciate it.
$artist=$_POST['artist'];
echo $artist . "<br>";
$safeartist = mysqli_real_escape_string($artist);
echo $safeartist . "<br>";
$title=$_POST['title'];
$safetitle = mysql_real_escape_string($title);
echo $safetitle . "<br>";
It does in fact have the data in the 'echo artist' command but does not for the second echo.
If you are trying to print the string in the DOM it might be that you are looking for htmlentities
http://www.php.net/htmlentities
mysql_* is deprecated PDO is often a more reasonable choice http://php.net/PDO
I'm current making a website with a photo album in it. The website in 2 languages, english and dutch. So I made language file like:
$lang['hello'] = 'Hallo'; //Hallo is hello in dutch
With the photo album I'm trying use the same principle like:
$lang['discription_001'] = 'photo of a house';
With showing the images I made a counter, now I want to use the same counter in the dispription like so:
echo $lang['discription_'$counter]
And $counter being 001 for photo number one. However this does not work, Could someone tell how I could get this to work, or any other method to get what I want.
Thanks in advance, Thomas de Zeeuw
P.S. I'm new in PHP, however I normally pick up things quite fast, so make some explaintion would be appreciated.
You're almost there:
echo $lang['discription_' . $counter]
The . is PHP's concatenation operator, for combining strings.
You just forgot the string concatenation operator . to concatenate 'discription_' and the value of $counter:
$lang['discription_'.$counter]
You have a typo.
echo $lang['discription_'$counter]
Should be
echo $lang['discription_' . $counter];
You could get it to work by simple repairing your code:
echo $lang['discription_'.$counter];
or
echo $lang["discription_{$counter}"];
Is $counter a string?? If so your example should work fine if you fix the parse error (You missed the concatenation operator (.).
It would be much better if you showed us actual code from your application.
echo $lang['discription_' . $counter];
As you have got answers already you forgot the concatenation operator, but better way in my mind would be to use multidimensional array.
echo $lang['descriptions'][$counter];
Am trying to pass the value hrough the URL but when i retrieve it it appears like this ' , urlencode(15.99),'
the value is correct but i have tried different things but still unable to just send the value without the urlencode or added syntax
the line of code am trying to send the value is
redirect_to("$the_file_is?subj=' . urlencode($the_price_is)' ");
This should do what you're looking for, assuming I understood it correctly:
redirect_to($the_file_is . '?subj=' . urlencode($the_price_is));
Surely just change to...
redirect_to($the_file_is."?subj=" . urlencode($the_price_is));
Almost there - just a little bit of quote confusion :)
redirect_to("$the_file_is?subj=" . urlencode($the_price_is));
It's worth noting that you can reference variables in double quotes so:
echo "hello $name"; will work, but echo "$actioned"; might need to be echo "{$action}ed"; echo $action.'ed'; depending.
I seriously suck at getting the quotes and quotation marks correctly. is there a rule that would help me easily remember?
echo "<option value='".$data['recordc']."' . "selected=selected>" '". data['recordc'] ."' . "</option>";
You could easily fix the quotes problem with a decent editor that has syntax highlighting (even StackOverflow's basic highlighting quickly shows where you went wrong) but that is still a horrible mess to read.
Most of the time if you find yourself outputting too much HTML in an echo you are probably best served to do it in a cleaner way, like breaking out of PHP. I would do at the very least:
printf('<option value="%1$s" selected=selected>%1$s</option>', $data['recordc']);
In general you can use this instead:
echo "<option value=\"$data[recordc]\" selected=selected>$data[recordc]</option>";
which is a little easier to read. Array keys don't need to be quoted when used inside a string.
In this case, paolo's printf method is nice since the variable is reused