orbitalnets Posted March 13, 2008 Share Posted March 13, 2008 Dear All, I am trying to assing a string to a variable like this: $link = "\'<p>\' $row_show_portfolio['url'] \'</p>'"; But I get: PHP Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE But If i escape the url like this $link = "$row_show_portfolio[\'url\']"; I will get a problem as well. I tried this also: $link = "{$row_show_portfolio['url']}"; What is the correct way to do this? Regards, Dwayne Link to comment https://forums.phpfreaks.com/topic/95957-assign-a-string-to-a-variable-newbe-problem/ Share on other sites More sharing options...
conker87 Posted March 13, 2008 Share Posted March 13, 2008 You don't need to escape the ' since you're using double quotes. Try: <?php $link = "'<p>' ". $row_show_portfolio['url'] ."'</p>'"; ?> Link to comment https://forums.phpfreaks.com/topic/95957-assign-a-string-to-a-variable-newbe-problem/#findComment-491227 Share on other sites More sharing options...
johnska7 Posted March 13, 2008 Share Posted March 13, 2008 You need concatenation, and quotes only need to be escaped (\") inside of other quotes So what you want is: $link = "<p>".$row_show_portfolio['url']."</p>"; If you wanted to have quotes in your string, then you'd need to do: $link = "<p>He said:\"I'm a nice guy!\" ".$row_show_portfolio['url']."</p>"; the above would print out (to the $link variable): "I'm a nice guy" your_row_return_here Link to comment https://forums.phpfreaks.com/topic/95957-assign-a-string-to-a-variable-newbe-problem/#findComment-491228 Share on other sites More sharing options...
soycharliente Posted March 13, 2008 Share Posted March 13, 2008 $link = "{$row_show_portfolio['url']}"; What was wrong with that? It looks fine to me. Link to comment https://forums.phpfreaks.com/topic/95957-assign-a-string-to-a-variable-newbe-problem/#findComment-491244 Share on other sites More sharing options...
orbitalnets Posted March 13, 2008 Author Share Posted March 13, 2008 hmm ok this can be really confusing it it is a variable mixed with html: $link = "<a href=\" ".$row_show_portfolio['url'].\" " "target=\"_blank\"><img src=\"" .$row_show_portfolio['image']. "\" alt=\"" .$row_show_portfolio['client']. "\" /></a>"; Like this? Link to comment https://forums.phpfreaks.com/topic/95957-assign-a-string-to-a-variable-newbe-problem/#findComment-491487 Share on other sites More sharing options...
lordfrikk Posted March 13, 2008 Share Posted March 13, 2008 If you have complex string or text you want to assign to variable without need of escaping everything inside, you might want to assign it via HEREDOC like this: <?php $str = <<<DELIMITER Example of 'string' spanning "multiple" lines using heredoc syntax without need to escape strings. DELIMITER; // $str now contains text in between <<<DELIMITER and DELIMITER ?> Link to comment https://forums.phpfreaks.com/topic/95957-assign-a-string-to-a-variable-newbe-problem/#findComment-491497 Share on other sites More sharing options...
soycharliente Posted March 13, 2008 Share Posted March 13, 2008 You missed a quote. But this should be fine. $link = "<a href=\"{$row_show_portfolio['url']}\" target=\"_blank\"> <img src=\"{$row_show_portfolio['image']}\" alt=\"{$row_show_portfolio['client']}\" /> </a>"; Link to comment https://forums.phpfreaks.com/topic/95957-assign-a-string-to-a-variable-newbe-problem/#findComment-491500 Share on other sites More sharing options...
orbitalnets Posted March 13, 2008 Author Share Posted March 13, 2008 Ok I tried it with this one: $link = <<<DELIMITER <a href="$row_show_portfolio['url']" target="_blank"><img src="$row_show_portfolio['image']" alt="$row_show_portfolio['client']" /></a> DELIMITER; PHP Parse error: syntax error, unexpected T_SL on line (the same line where the $link is) Could there be anther problema than the notation? like the variables it self? Also I have show errors turn off in my php config. But If I put error_reporting(E_ALL); just before the funtion I still can not see the problem to troubleshoot this. Here is my server config: http://www.orbitalnets.com/info.php Regards, Dwayne Link to comment https://forums.phpfreaks.com/topic/95957-assign-a-string-to-a-variable-newbe-problem/#findComment-491525 Share on other sites More sharing options...
revraz Posted March 13, 2008 Share Posted March 13, 2008 You dont need to use double quotes in double quotes for HTML, it's fine to use single quotes in double quotes. hmm ok this can be really confusing it it is a variable mixed with html: $link = "<a href=\" ".$row_show_portfolio['url'].\" " "target=\"_blank\"><img src=\"" .$row_show_portfolio['image']. "\" alt=\"" .$row_show_portfolio['client']. "\" /></a>"; Like this? Link to comment https://forums.phpfreaks.com/topic/95957-assign-a-string-to-a-variable-newbe-problem/#findComment-491533 Share on other sites More sharing options...
lordfrikk Posted March 14, 2008 Share Posted March 14, 2008 Ok I tried it with this one: $link = <<<DELIMITER <a href="$row_show_portfolio['url']" target="_blank"><img src="$row_show_portfolio['image']" alt="$row_show_portfolio['client']" /></a> DELIMITER; PHP Parse error: syntax error, unexpected T_SL on line (the same line where the $link is) Could there be anther problema than the notation? like the variables it self? Also I have show errors turn off in my php config. But If I put error_reporting(E_ALL); just before the funtion I still can not see the problem to troubleshoot this. Here is my server config: http://www.orbitalnets.com/info.php Regards, Dwayne Dwayne, there needs to be a new line after the initial <<<DELIMITER as in the example. Also, when you include associative array variables in the string, instead of $array['key'] use $array[key]. This is how your string should look like: <?php $link = <<<DELIMITER <a href="$row_show_portfolio[url]" target="_blank"><img src="$row_show_portfolio[image]" alt="$row_show_portfolio[client]" /></a> DELIMITER; ?> Link to comment https://forums.phpfreaks.com/topic/95957-assign-a-string-to-a-variable-newbe-problem/#findComment-492026 Share on other sites More sharing options...
soycharliente Posted March 14, 2008 Share Posted March 14, 2008 Also, when you include associative array variables in the string, instead of $array['key'] use $array[key]. I don't want to rock the boat, but I've read and been told, well lots of times, that you need to put quotes around the key. There is a default way that it will be handled and that proper coding practice is to use quotes. I'm not saying that you are wrong and I'm not saying that you are right. I'm not able to find anything that argues either way when I dive in and RTM. I remember seeing a moderator posting a comment about the subject, but I can't seem to find it anymore. Link to comment https://forums.phpfreaks.com/topic/95957-assign-a-string-to-a-variable-newbe-problem/#findComment-492040 Share on other sites More sharing options...
lordfrikk Posted March 14, 2008 Share Posted March 14, 2008 Also, when you include associative array variables in the string, instead of $array['key'] use $array[key]. I don't want to rock the boat, but I've read and been told, well lots of times, that you need to put quotes around the key. There is a default way that it will be handled and that proper coding practice is to use quotes. I'm not saying that you are wrong and I'm not saying that you are right. I'm not able to find anything that argues either way when I dive in and RTM. I remember seeing a moderator posting a comment about the subject, but I can't seem to find it anymore. Yes, I agree, charlie. On the other hand, try addin single quotes around the keys in the above example... for a reason unknown to me it gives me this: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING Link to comment https://forums.phpfreaks.com/topic/95957-assign-a-string-to-a-variable-newbe-problem/#findComment-492055 Share on other sites More sharing options...
soycharliente Posted March 14, 2008 Share Posted March 14, 2008 On the other hand, try addin single quotes around the keys in the above example If you wanted to use that code: <?php $link = <<<DELIMITER <a href="$row_show_portfolio['url']" target="_blank"> <img src="$row_show_portfolio['image']" alt="$row_show_portfolio['client']" /> </a> DELIMITER; ?> You'd have to use variables with keys that are wrapped in brackets. You want it to evaluate itself and not, I guess, do whatever it did to you. I tested this code, not this exact code BTW but on my own site, and it works: <?php $link = <<<DELIMITER <a href="{$row_show_portfolio['url']}" target="_blank"> <img src="{$row_show_portfolio['image']}" alt="{$row_show_portfolio['client']}" /> </a> DELIMITER; ?> Link to comment https://forums.phpfreaks.com/topic/95957-assign-a-string-to-a-variable-newbe-problem/#findComment-492204 Share on other sites More sharing options...
lordfrikk Posted March 14, 2008 Share Posted March 14, 2008 Thanks for pointing that out! Link to comment https://forums.phpfreaks.com/topic/95957-assign-a-string-to-a-variable-newbe-problem/#findComment-492304 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.