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 9 of 9
  1. #1
    New Coder
    Join Date
    Mar 2010
    Location
    USA, NJ
    Posts
    42
    Thanks
    16
    Thanked 0 Times in 0 Posts

    conditional with no curly brackets

    I came across this function, and I don't understand how you can have a conditional statement without any curly brackets. Can someone give me and explanation as to why and how this works.

    PHP Code:
    // Strip Input Function, prevents HTML in unwanted places
    function stripinput($text) {
        if (
    ini_get('magic_quotes_gpc'))
        
    $text stripslashes($text);
        
    $search = array("\"""'""\\"'\"'"\'""<"">""&nbsp;");
        
    $replace = array("&quot;""'""\", "&quot;", "", "&lt;", "&gt;", " ");
        $text = str_replace($search, $replace, $text);    
        return $text;

    If I include the curly brackets where I instinctively think they should go as shown below it does not replace the $search values with the $replace values.

    PHP Code:
    // Strip Input Function, prevents HTML in unwanted places
    function stripinput($text) {
        if (
    ini_get('magic_quotes_gpc'))   {
        
    $text stripslashes($text);
        
    $search = array("\"""'""\\"'\"'"\'""<"">""&nbsp;");
        
    $replace = array("&quot;""'""\", "&quot;", "", "&lt;", "&gt;", " ");
        $text = str_replace($search, $replace, $text);    
           }
        return $text;

    Thank you

  • #2
    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
    Conditions without curly braces only apply to the next line in the code. I personally do not omit the curly braces regardless of if its one line due to the readability aspect.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New Coder
    Join Date
    Mar 2010
    Location
    USA, NJ
    Posts
    42
    Thanks
    16
    Thanked 0 Times in 0 Posts
    Hi Fou-Lu,

    So the following would be the correct representation using brackets?


    PHP Code:
    // Strip Input Function, prevents HTML in unwanted places
    function stripinput($text) {
        if (
    ini_get('magic_quotes_gpc'))   {
        
    $text stripslashes($text);
            }
        
    $search = array("\"""'""\\"'\"'"\'""<"">""&nbsp;");
        
    $replace = array("&quot;""'""\", "&quot;", "", "&lt;", "&gt;", " ");
        $text = str_replace($search, $replace, $text);    
        return $text;


  • #4
    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
    Thats correct. Its also writable as ternary:
    PHP Code:
    // or can call get_magic_quotes_gpc() which returns a boolean
    $text ini_get('magic_quotes_gpc') ? stripslashes($text) : $text

    The search and replace seems a little overboard though. You can run that through htmlspecialchars or htmlentities instead of doing the work yourself. It looks like a syntax error on the forums here, but I'm betting you that its no problem at all.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    steadythecourse (07-30-2010)

  • #5
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    the placement of the curly brackets may be correct but the reason the function is not working is because you have unclosed quotes

    PHP Code:
    "\"   // string is not closed because of the escape char \" 
    PHP Code:
    "\\" // this string is closed. 
    You can not say you know how to do something, until you can teach it to someone else.

  • #6
    New Coder
    Join Date
    Mar 2010
    Location
    USA, NJ
    Posts
    42
    Thanks
    16
    Thanked 0 Times in 0 Posts
    Thanks Fou-Lu,

    It works fine, and I like the ternary version.

    steadythecourse

  • #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
    Quote Originally Posted by timgolding View Post
    the placement of the curly brackets may be correct but the reason the function is not working is because you have unclosed quotes

    PHP Code:
    "\"   // string is not closed because of the escape char \" 
    PHP Code:
    "\\" // this string is closed. 
    I noticed that too, but I'm thinking that its a strip from the forum. I've seen a very similar problem to this not that long ago which causes the pattern typed replacements to show incorrectly when parsed by the [php] tags.
    Although for the OP, if it actually is like that (just the single), then yes its definitely a syntax error and needs to be corrected. They can of course just be removed since it searches and replaces the same char.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #8
    New Coder
    Join Date
    Mar 2010
    Location
    USA, NJ
    Posts
    42
    Thanks
    16
    Thanked 0 Times in 0 Posts
    Yes,

    the forum is causing the problem. I can't get it to display correctly.

    thanks again

  • #9
    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
    Quote Originally Posted by steadythecourse View Post
    Yes,

    the forum is causing the problem. I can't get it to display correctly.

    thanks again
    I have yet to determine WHAT it is that PHP is doing when it destroys those, but can make it difficult if your trying to debug things like regex when you can't see it properly >.<
    Thanks for confirming that.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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