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
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using a { and } in a function

    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?

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    This little Earth.
    Posts
    383
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    'If you don't stand for something, you'll fall for anything.'

  • #3
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,058
    Thanks
    10
    Thanked 96 Times in 94 Posts
    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.

    PHP Code:
    <?
    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...

    PHP Code:
    <?
    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.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think I get it, would that mean that

    PHP Code:
    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?

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,058
    Thanks
    10
    Thanked 96 Times in 94 Posts
    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
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #7
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I understood what you meant there firepages.


  •  

    Posting Permissions

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