...

View Full Version : Using a { and } in a function



missing-score
04-06-2003, 09:07 PM
I have seen curled brackets ( {,} ) used in some functions, especially the imap and fsockopen functions.

I just wondered what purpose these served.

Can anyone help me? :D

ASAAKI
04-06-2003, 09:55 PM
the curly brackets are used to open and close all functions.
like so:

function abc(){
...
whatever happens in the function....
...
}

they're also used to start and end if blocks and loops.
eg.
for(whatever){
the loop
}

and
if(condition){
...whatever...
}
see?

or did you mean something else? :confused:

missing-score
04-06-2003, 10:02 PM
I know that ( sorry, guess I didnt make myself clear )

This may not be where it should be, but it looks like it:

imap_open('{my.imap.host}','user','pass');


I dont use the imap functions, and this is probably incorrect, but have seen it used in a similar way and with mysql functions.

firepages
04-07-2003, 06:51 AM
the curly braces are used ..(from php4.1 ? perhaps 4.2 onwards) to tell PHP that the enclosed value should be treated as a variable and not as a literal...

i.e.



<?
mysql_query("SELECT * FROM $table WHERE id=$_POST['id']")or die(mysql_error());

//would die ... but this .. would work

mysql_query("SELECT * FROM $table WHERE id={$_POST['id']}")or die(mysql_error());
?>


in a similar fashion ..turn up error reporting...



<?
error_reporting(E_ALL);

$_GET['world']='world';
echo "hello $_GET['world']";

/*PHP looks for the $_GET variable 'world' (quotes and all)
which does not exist , braces force the comparison..*/

echo "hello {$_GET['world']}";
?>


thats a bad example but shows the point, note in the above ..

echo "hello $_GET[world]";

would print 'hello world' , but also throw a warning.

missing-score
04-07-2003, 08:46 AM
I think I get it, would that mean that


mysql_connect('{$host}','user','pass');


would find a variable named $host even though it is in single quotes, or do you need to use double quotes?

firepages
04-07-2003, 09:51 AM
no, anything in single quotes is taken as literal so above php would look for a host called {$host}

mysql_connect("$host",etc

is fine but pointless as
mysql_conect($host,etc

would do the job as-is , but
mysql_conect("$config['host']",etc

will fail as php looks for a literal $config=>'host'
whereas with
mysql_conect("{$config['host']}",etc

PHP now parses the above as if you had written ...
mysql_connect(" ".$config['host']." ",etc i.e. $config=>host

err ok a better example ;) this
echo "localhost config is {$config['host']} and this ...blah";

would be interpreted by PHP internally as if you had written
echo "localhost config is ".$config['host']." and this ...blah";

...I know what I mean I just don't appear to know how to say it ;)

missing-score
04-07-2003, 05:54 PM
I understood what you meant there firepages.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum