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 8 of 8

Thread: if then help

  1. #1
    Regular Coder
    Join Date
    Sep 2008
    Location
    Oklahoma
    Posts
    249
    Thanks
    11
    Thanked 13 Times in 13 Posts

    Question if then help

    This may seem stupid, but I can only do basic if then statements.

    What I dont know how to code is if variable has data then another variable equals something.

    Im using variables in a config.php, and i need code to determine if a variable has any data, then makes a new variable have data, but if teh first variable is empty, then the new variable in empty.

    Please help me, OwT
    Last edited by owt200x; 10-19-2008 at 11:16 PM.

  • #2
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Code:
    if ($a == "1")
    {
    //Do code
    }
    
    elseif ($a == "2")
    {
    //Do code
    }
    
    elseif ($a == "3")
    {
    //Do code
    }
    
    else {
    //Do code
    }
    You can have as many elseif's as you want. But only 1 IF and 1 ELSE for a certain variable.

  • #3
    Regular Coder
    Join Date
    Sep 2008
    Location
    Oklahoma
    Posts
    249
    Thanks
    11
    Thanked 13 Times in 13 Posts
    that would work, but i need it to determine if the variable has a value at all. then do code. I wont know what the variables value will be, so i wouldnt be able to get it to work with that
    Last edited by owt200x; 10-18-2008 at 11:46 PM. Reason: needed to clarify response

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Where are the values coming from? Provide us with more information. Your post was pretty vague to begin with.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #5
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    and i need code to determine if a variable has any data, then makes a new variable have data, but if teh first variable is empty, then the new variable in empty.
    Check if variable has any value.

    Code:
    echo "$value";
    if variable is empty

    Code:
    if ($value == "")
    {
    //Its empty
    }
    else {
    //Its not empty
    }

    Or you could do this. To change a variable if its empty into one with data.

    Code:
    if ($value == "")
    {
    $value = 1;
    }

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by owt200x View Post
    that would work, but i need it to determine if the variable has a value at all. then do code. I wont know what the variables value will be, so i wouldnt be able to get it to work with that
    the variable exists?
    PHP Code:
    if(isset($namevar)){
      print 
    "is true => exists";
    }else{
      print 
    "is false => not exists";

    is variable empty?
    PHP Code:
    if(empty($namevar)){
      print 
    "yes";
    else
      print 
    "no"
    is variable not empty? (negate something using ! operator)
    PHP Code:
    if(!empty($namevar)){
      print 
    "no"// because not true => false
    else
      print 
    "yes"
    variable exists and is not empty?
    PHP Code:
    if(isset($namevar) && !empty($namevar)){
      print 
    "yes";
    }else{
      print 
    "no";

    the best way to learn something is the manual:
    language reference
    control structure, if, for, ...
    operators, empty, isset, !, &&, ||, and, or, not,....
    when you are stuck with something ask, always is somebody who help you to make things clear,

    best regards

  • Users who have thanked oesxyl for this post:

    owt200x (10-19-2008)

  • #7
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Whats great about PHP is there is a dozen ways of writing the same piece of code using different methods.

  • #8
    Regular Coder
    Join Date
    Sep 2008
    Location
    Oklahoma
    Posts
    249
    Thanks
    11
    Thanked 13 Times in 13 Posts

    Smile

    Quote Originally Posted by oesxyl View Post
    PHP Code:
    if(empty($namevar)){
      print 
    "yes";
    else
      print 
    "no"
    I appologize my question wasn't very clear, i didn't exactly know how to put what i needed, but this is what i needed. thank you all for helping.

    btw, the variables would be included from another php file, for config settings in the main script.

    Last edited by owt200x; 10-19-2008 at 10:57 PM. Reason: added where variables came from


  •  

    Tags for this Thread

    Posting Permissions

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