php,mysql , and smarty. Confused by a "{" in social-engine platform
hi I was looking at a php code from social engine and they have something like this:
header("Location:user_event.php?event_id={$event_id}&justadded=1")
why id it not
h开发者_开发百科eader("Location:user_event.php?event_id=$event_id&justadded=1")
or
header("Location:user_event.php?event_id=".$event_id."&justadded=1")
because the value of $event_id is correct but when the page redirects I go to:
user_event.php?event_id=&justadded=1
now I'm sure I did something to mess the value of {$event_id} but I don't even know what it means. does it have to do with smarty?
Using {$variable} inside of double quoted strings, is for two reasons: a) to prevent mistakes like:
$variable = "prefix";
header("Location:somepage.php?value=$variable_name"); // PHP would actually look for a variable called $variable_name instead of the desired $variable
b) allowing to insert variables of arrays and objects into the string without interrupting it
header("Location:somepage.php?value=$myObject->someValue"); // this wouldn't work
header("Location:somepage.php?value={$myObject->someValue}"); // this works
But all of this shouldn't have any effect with Smarty, because Smarty only parses { } entities inside of the template files and header("Location: ....");
definitly doesn't belong there, unless you have a {php}header("Location:...");{/php}
If you have the later one, than you have to access the $event_id differently, because it's not accessible from inside the Smarty class, unless you assign it first with
$smarty->assign('event_id', $event_id);
than either
{php}
$event_id = $this->get_template_vars('event_id');
header("Location:user_event.php?event_id={$event_id}&justadded=1");
{/php}
or
{php}
global $event_id;
header("Location:user_event.php?event_id={$event_id}&justadded=1");
{/php}
But having this kind of code inside the template is quite wrong. Usually such stuff should be done in the actuall php file before the template is ever called.
精彩评论