Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Newbee question

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Newbee question

Postby garyw74 » Wed Oct 09, 2002 6:37 am

Hi

Can anybody point me in the right direction Im very new to php and any help would be appreciated

Whats wrong with this snippt of code??

<?php
echo '<img src="catalog/affiliates/$_SERVER['SERVER_NAME']/oscommerce.gif">' ;

?>

I get the following error

unexpected T_STRING, expecting ',' or ';'

Thanks in advance
garyw74
New php-forum User
New php-forum User
 
Posts: 5
Joined: Wed Oct 09, 2002 6:22 am

Postby Xerpher » Wed Oct 09, 2002 6:48 am

Your string has single quotes at the ends AND you have single quotes inside your string... thats bad :)

Try this...
Code: Select all
<?php

echo '<img src="catalog/affiliates/'.$_SERVER['SERVER_NAME'].'/oscommerce.gif">' ;

?>


I'm pretty sure that'll fix your problem :D
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Thanks

Postby garyw74 » Wed Oct 09, 2002 7:01 am

Thanks very much :D
garyw74
New php-forum User
New php-forum User
 
Posts: 5
Joined: Wed Oct 09, 2002 6:22 am

Postby Jay » Wed Oct 09, 2002 2:16 pm

The main difference between using single quotes (') and double quotes(") is that single quotes will quote a string exacty:
print 'The variable value is $value'; will output The variable value is $value
Double quotes will parse the string for variables and replace them, therefore (assuming $value = 50) using double quotes for the same line will output The variable value is 50

You also can't use the same style quote within your string (otherwise the parser wouldn't know where your string ended). If you have to use the same quote, escape it with a backslash first so it's not taken literally (or use double quotes if you're using single - I personally find using double quotes far easier)
eg print 'That's Amazing' - Wrong
print 'That\'s Amazing' - Correct
print "That's Amazing" - Even better

You also cannot use an array variable within a string, unless you either access it directly in PHP (ie close the string, concatenate the variable and reopen the string) or enclose it within curly braces
eg - print "The value is $_SESSION['value']"; - Wrong
print "The value is {$_SESSION['value']}"; - OK
print "The value is ".$_SESSION['value']; - Much better
Jay
 

Thanks

Postby garyw74 » Thu Oct 10, 2002 10:37 pm

Great, that really helps.
Thanks for the advice
garyw74
New php-forum User
New php-forum User
 
Posts: 5
Joined: Wed Oct 09, 2002 6:22 am


Return to PHP coding => General

Who is online

Users browsing this forum: Google Feedfetcher and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron