Jump to content

assign a string to a variable - newbe problem


orbitalnets

Recommended Posts

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

 

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

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?

 

 

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
?>

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

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?

 

 

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

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.

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.