Re: PHP scripts and IE

From: Ashmodai (ashmodai_at_mushroom-cloud.com)
Date: 01/30/05

  • Next message: Ashmodai: "Re: autocomplete"
    Date: Sun, 30 Jan 2005 03:24:49 +0100
    
    

    Geoff Berrow scribbled something along the lines of:
    > I noticed that Message-ID: <cthahu$7gm$1@sparta.btinternet.com> from
    > Tony Wainwright contained the following:
    >
    >
    >>Sorry Geoff, just learning - if you could point out what is wrong with the
    >>code I would appreciate it. And perhaps suggest a better book (I am using
    >>PHP, MySQL and Apache All in One, published by Sams)
    >
    >
    > Actually it's not bad enough to cause the error but I think it could
    > lead you into bad habits. I find it easier to be consistent in my use
    > of superglobals and use them like so :- $_POST['user']
    >
    > If you tried to do
    > echo $_POST[user];
    > You would get a warning notice. It only works in your case because it
    > is contained within double quotes. I find it simpler to use just one
    > way. Unfortunately this means you have to write the lines as:-
    >
    > echo "<p>Welcome <b>".$_POST['user']."</b>!</p>";
    > echo "<p>Your message is:<br><b>".$_POST['message']."</b></p>";
    > But it saves you having to think of whether to use single quotes or not.
    >
    > Also, it's a good idea to check if variables exist before using them. I
    > know this script won't get called unless the POST variables exist, but
    > again it's all about good habits.

    Actually the most sensible code would be:

    echo '<p>Welcome <b>', $_POST['user'], '</b>!</p>';
    echo '<p>Your message is:<br>
    <b>', $_POST['message'], '</b></p>';

    Single quotes mean that the content will be printed as-is. Double quotes
    mean the content might contain magic (variables, etc) which should be
    parsed. Also whitespace behaves differently from what you'd expect when
    you're using double quotes.
    Thus single quoted strings are processed faster.

    Using the dot operator leads to the string being combined before being
    echoed, the comma will lead to every single value being echoed on its
    own (as both things lead to the same output the latter should be
    preferred because it's faster).

    You should always check whether the array key exists before you use it
    (array_key_exists). Then you should usually perform a value check if
    you're going to do anything with it.

    Of course the best solution if you have lots of text to output is not to
    run the static part through PHP in the first place, so:

    <p>Welcome <b><?php echo $_POST['user']; ?></b>!</p>
    <p>Your message is:<br>
    <b><?php echo $_POST['message']; ?></b></p>

    would be the best solution IMO (although you might want to make it
    multilingual later on, but I won't go into that).

    --
    Ashmo
    

  • Next message: Ashmodai: "Re: autocomplete"

    Relevant Pages

    • Re: update hyperlink through query
      ... I tried it with double quotes first but someone told me single quotes ... My name's common all over the world. ... I have created a table in a database that contains hyperlinks pointing ...
      (microsoft.public.access.queries)
    • Re: nesting JS in echos
      ... converted from html to php. ... syntax of the single quotes and double quotes needed in the ...
      (comp.lang.php)
    • Re: br html line breaks and htmlentitities
      ... when reading user-supplied input I use 'addslashes' ... display, and so single quotes are not visually escaped, ...
      (comp.lang.php)
    • Re: Sending PHP variables through echo "
      "
      ... First error: using single quotes instead of double quotes. ... RTFM on the string type, specially on the subject of variable expansion. ...
      (comp.lang.php)
    • Mandis Quotes (aka retiring """ and )
      ... arbitrary textual matter called "Mandis quotes". ... surround the string by a pair of doubled single quotes. ... of ASCII or Unicode characters, but instead as a sequence of lines ...
      (comp.lang.python)