Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Am I suppose to brake out of this?

    2 x
    unexpected T_ENCAPSED_AND_WHITESPACE
    PHP Code:
    $query "select zone from delivery where country = '$_POST['country']'"
    Am I suppose to be braking out of '$_POST['country']' or something?

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,344
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    PHP Code:
    $query "select zone from delivery where country = '$_POST[country]'"
    You don't use the single quotes in an array inside double quotes.

    Your SQL is formed like this "select zone from .." - Note the double quotes.

    So.. when an array is used inside double quotes you don't use the single quotes inside it. Because we're already inside double quotes PHP knows that it should inspect variables including arrays and it will replace them in the SQL with their real value.

    You could also use {$_POST['country']} in the SQL instead.

    For more info about quotes and the T_WHITESPACE error see the link in my signature about quotes.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    FlashDance (10-08-2011)

  • #3
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,454
    Thanks
    71
    Thanked 102 Times in 101 Posts
    PHP Code:
    $query "SELECT zone FROM delivery WHERE country='".$_POST['country']."'"

  • Users who have thanked myfayt for this post:

    FlashDance (10-08-2011)

  • #4
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts
    tangoforce, I did not know that about the single quotes inside double quotes. You explained it really well. I've had four people try to explain that to me, but it didn't get through. Yours did. Thanks for the info mate!

    myfayt, I feel like the biggest idiot! I could have swore I used that combination of quotes and full stops, infact, I am sure it was the first attempt I made to brake out! Anyhow, I must have... I donno, I'm a big goose! Thanks man!

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts
    Man, I'm fkn FUMING! 25 minutes lost because of my stupidity!

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,344
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    Quote Originally Posted by FlashDance View Post
    myfayt, I feel like the biggest idiot! I could have swore I used that combination of quotes and full stops, infact, I am sure it was the first attempt I made to brake out! Anyhow, I must have... I donno, I'm a big goose! Thanks man!
    Yes you did try that combination.. but you forgot to wrap it in {} braces which tell php to treat it normally - as if it were not inside double quotes.

    Don't get annoyed with yourself, its part of learning. When I was first learning I spend HOURS and sometimes DAYS trying to work out why my code was showing errors.. only to find i'd missed a semicolon. It's just one of those things you learn from.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Using {$_POST['country']} would be the more accurate way to represent this within a string. This is referred to as complex evaluation.
    This would be done for consistency only.
    Constants in PHP are not parsed within the context of a string. I personally recommend breaking out of strings completely (or using a formatter which I usually do) instead of doing either of the above. Functions and methods also require complex evaulation.
    Here's an example as to how easy it would be to err:
    PHP Code:
    <?php
    define
    ('T''C');
    $a = array('T' => 'Test''C' => 'Cat');

    print 
    "This is a string with $a[T] in it.<br />" PHP_EOL;
    print 
    "This is a string with {$a['T']} in it.<br />" PHP_EOL;
    print 
    "This is a string with {$a[T]} in it.<br />" PHP_EOL;

    class 
    Obj
    {
        private 
    $v;
        public function 
    __construct($in)
        {
            
    $this->$in;
        }

        public function 
    p()
        {
            return 
    $this->v;
        }
    }

    $o = new Obj('test');

    print 
    "This is a string with {$o->p()} in it.<br />" PHP_EOL;
    print 
    "This is a string with $o->p() in it.<br />" PHP_EOL;

    ?>
    Results in:
    Code:
    This is a string with Test in it.<br />
    This is a string with Test in it.<br />
    This is a string with Cat in it.<br />
    This is a string with test in it.<br />
    This is a string with () in it.<br />


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •