--- In [email protected], "Bob" <[EMAIL PROTECTED]> wrote:
>
> > On 10/10/07, Adrian Suri <adrian.suri@ wrote:
> >  New to php and wandered what I was doing wrong, according to my
PHP book
> >  to get user agent I should do this
> >  <?PHP
> >  print "your Browser is: $_SERVER['HTTP_USER_AGENT']"
> >  ?>
>  
> Hi Adrian,
> You got 2 answers, but you may not know why.
> You can echo or print a variable within quotes ok.
> Example echo "Hello $name";
> This could also be written: "Hello " . $name; or 'Hello ' . $name;
> 
> But, $_SERVER['HTTP_USER_AGENT'] is an array (the brackets denote this).
> Arrays need to be treated differently when you print them, confusing
isn't it?
> 
> It's a matter of personal preference how you do it.
> These will both work:
> print "your Browser is: {$_SERVER['HTTP_USER_AGENT']}";
> print 'your Browser is: ' . $_SERVER['HTTP_USER_AGENT'];
> 
> The curly brackets allow an array to be used within printed quotes.
> 
> My preference is to use the 2nd method, as a colour coded editor
shows this in a difference color, so you know you've spelt it
correctly, but you will find your own style that you prefer.
> 
> It is said that by using single quotes, that php is faster, as it
doesn't need to peruse the string looking for variables, but we are
talking about ridiculous times, so you won't see any difference unless
it's in a loop of maybe 1000's.
> Example: 'Hello ' . $name; is supposed to be faster than "Hello $name";
> 
> I've never done any timing myself, so I may have opened a can of
worms here?
> 
> I *try* to use code that is easily readable, so it won't make a bit
of difference to normal coding.
> Hope I've helped a bit.
> Regards, Bob.
>

This is good advice. I tend to use another format when echoing or
printing out arrays such as $_SERVER['HTTP_USER_AGENT']. Why not just
make that a variable, then echo it out?

eg.

<?php

$agent = $_SERVER['HTTP_USER_AGENT'];

echo "Your browser is:" . $agent;

?>

By the way, always make sure you have a semicolon when required. I
believe that was one of the reasons why your script didn't work.



Reply via email to