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
    Regular Coder
    Join Date
    Nov 2002
    Posts
    161
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Check if a variable exists

    if (FILE_EXISTS)

    Is there any way to check "if (VARIABLE_EXISTS)"?

  • #2
    New Coder
    Join Date
    Mar 2003
    Location
    Somewhere far beyond
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    if(isset($var)) do_something(); 
    WBR, Weirdan.

  • #3
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can just put

    if($var)

    to check if a variable exists.

  • #4
    Regular Coder
    Join Date
    Nov 2002
    Posts
    161
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by missing-score
    You can just put

    if($var)

    to check if a variable exists.
    That's only true if you have "~E_NOTICE" in your error handling config. If you are showing notices then you still get a warning for non-existent vars, unless you test for isset()

    I don't know for a fact, but I presume that strict coding is more efficient than loose, as it is with JS. Can anyone confirm or refute that?
    Last edited by brothercake; 04-10-2003 at 07:43 PM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #6
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I use strict coding aswell, and I do always use isset(), because it makes it easier to read.

    I was just saying that you can test for variables like that.

    Sorry if you misunderstood! Its not my day today

  • #7
    TFP
    TFP is offline
    New to the CF scene
    Join Date
    Mar 2003
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you want to look if the field of a form is filled out you should use the empty-function

    PHP Code:
    if (empty($var)) 
    because the variable is set but the value is NULL


    TFP

  • #8
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nice one, I forgot all about empty function.

  • #9
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,045
    Thanks
    10
    Thanked 93 Times in 91 Posts
    Originally posted by brothercake
    I don't know for a fact, but I presume that strict coding is more efficient than loose, as it is with JS. Can anyone confirm or refute that?
    Yes , as you say PHP will generate a warning if you go if($var) and $var is undefined.
    The fact that PHP has to generate that warning (printed to screen or otherwiase) technically makes the script less efficient, we are talking millisecond or part of I am sure, but true non the less.
    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)


  •  

    Posting Permissions

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