Page 1 of 1
[Resolved] Syntax problem with anchored substr array.
Posted: Thu Feb 15, 2007 1:34 pm
by blepoxp
Hello,
I'm trying to strip the first 25 characters off of $row2[Doc_Location].
This is located in an html anchor:
Code: Select all
foreach ($cache2 as $row2)
{ if ($row2['Doc_ResID'] == $row['Res_ID'])
{$output .= "<li><a href="substr('$row2[Doc_Location]', 25)">$row2[Doc_Title]</a></li>";}
If I use a single quote in the html anchor, the substr is not interpreted by PHP.
If I use a double quote, I get a syntax error.
How would I go about accomplishing this?
Thanks for your help.
Re: Syntax problem with anchored substr within an array.
Posted: Thu Feb 15, 2007 1:40 pm
by blackbeard
Try this:
Code: Select all
foreach ($cache2 as $row2)
{ if ($row2['Doc_ResID'] == $row['Res_ID'])
{$output .= "<li><a href=\"".substr($row2[Doc_Location], 25)."\">".$row2[Doc_Title]."</a></li>";}
Posted: Thu Feb 15, 2007 1:43 pm
by blepoxp
That worked.
Would you mind explaining what happened there... Are those escapes? Is the . your typical "append"?
... and thank you.
Posted: Thu Feb 15, 2007 1:44 pm
by RobertGonzalez
Strings bro.
Code: Select all
<?php
foreach ($cache2 as $row2)
{
if ($row2['Doc_ResID'] == $row['Res_ID'])
{
$output .= '<li><a href="' . substr($row2['Doc_Location'], 25) . '">' . $row2['Doc_Title'] . '</a></li>';
}
}
?>
Posted: Thu Feb 15, 2007 1:49 pm
by blackbeard
String Operators
There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side. Please read Assignment Operators for more information.
Posted: Thu Feb 15, 2007 1:49 pm
by blepoxp
I will. Thank for the help and for the links.
Posted: Thu Feb 15, 2007 1:52 pm
by RobertGonzalez
Bear in mind that if you are not parsing a string with PHP vars in it, single quotes (literal strings) are going to be faster than double quotes (parsed strings). So I would suggest you use single quotes on all of your strings and concatenate vars into them as needed (which is still faster than parsing them directly in the string).
Code: Select all
<?php
$var = 'John Smith';
// You could do this...
$parse = "My name is $var";
// or you could do this, which is faster
$parse = 'My name is ' . $var;
?>