954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

String literial question

Hi,

I have the following simple test script:

<?php
   $links = array('URL'=>'www.google.com');
   print("$links['URL']"); // compilation error
   print("$links[URL]");
   print($links['URL']);
   print($links[URL]); // execution warning
?>


But two lines are giving me problems. Can any one help to explain?

Thanks.

--Mark

mark1048
Newbie Poster
12 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

Because they are using incorrect syntax. The correct syntax for retrieving an array value is:

print($links['URL']);

OR if you want to print the array value as part of a string then you can use:

print("This is a test url: {$links['URL']} : end of test");
Lafinboy
Junior Poster
172 posts since Jul 2004
Reputation Points: 16
Solved Threads: 7
 

The braces tell php that the string inside is a variable name, a nice safe way of getting the data
[php]print("{$links['URL']}");[/php]In an array it can't quite work out what to do here because it sees the array name and then doesn't know what to do with the square brackets
[php]print("$links['URL']"); // compilation error[/php]The lack of quotes around the key name help a bit but not as safe as the first example. This works, and is valid (as you've discovered) but I don't think it's best practice.[php]print("$links[URL]");[/php]The best way to do but sometimes they need to be built into a string and that's why the other methods exist. Note the single quotes around the 'URL', this tells PHP that the name isn't going to be anything fancy, just take the name and work with it.[php]print($links['URL']);[/php]It's looking for a defined, constant, variable called URL and not finding one. Because URL doesn't have a $ or quotes PHP looks for the special case and fails.[php]print($links[URL]); // execution warning[/php]

sarahk
Junior Poster
144 posts since Apr 2005
Reputation Points: 10
Solved Threads: 1
 

Thank you Sarah, for the good exlanation. Now I know array key should be single quoted all the time. If it's inside a double quoted string, put the array reference in {}.

One more question on:

print($links[URL]); // execution warning

Is there any setting to turn off the execution warning? I downloaded a PHP database application. It has a lots of lines like this to get values out of query results.

--Mark

mark1048
Newbie Poster
12 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

Take a look at the PHP manual for some options:

http://www.php.net/manual/en/ref.errorfunc.php

where you put something like this at the top of each page

[php]error_reporting(E_WARNING);[/php]

sarahk
Junior Poster
144 posts since Apr 2005
Reputation Points: 10
Solved Threads: 1
 

Sarah, thanks again...

mark1048
Newbie Poster
12 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You