[Resolved] Syntax problem with anchored substr array.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
blepoxp
Forum Newbie
Posts: 16
Joined: Tue Feb 13, 2007 12:13 pm

[Resolved] Syntax problem with anchored substr array.

Post 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.
Last edited by blepoxp on Thu Feb 15, 2007 1:50 pm, edited 1 time in total.
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Re: Syntax problem with anchored substr within an array.

Post 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>";}
blepoxp
Forum Newbie
Posts: 16
Joined: Tue Feb 13, 2007 12:13 pm

Post by blepoxp »

That worked.
Would you mind explaining what happened there... Are those escapes? Is the . your typical "append"?

... and thank you.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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>';
  }
}
?>
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post 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.
blepoxp
Forum Newbie
Posts: 16
Joined: Tue Feb 13, 2007 12:13 pm

Post by blepoxp »

I will. Thank for the help and for the links.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

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