...

View Full Version : Am I suppose to brake out of this?



FlashDance
10-08-2011, 12:57 PM
2 x
unexpected T_ENCAPSED_AND_WHITESPACE


$query = "select zone from delivery where country = '$_POST['country']'";

Am I suppose to be braking out of '$_POST['country']' or something?

tangoforce
10-08-2011, 01:21 PM
$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.

myfayt
10-08-2011, 01:24 PM
$query = "SELECT zone FROM delivery WHERE country='".$_POST['country']."'";

FlashDance
10-08-2011, 01:59 PM
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! :)

FlashDance
10-08-2011, 02:03 PM
Man, I'm fkn FUMING! 25 minutes lost because of my stupidity!

tangoforce
10-08-2011, 03:27 PM
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.

Fou-Lu
10-08-2011, 06:12 PM
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
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->v = $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:


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 />



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum