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 crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,096
    Thanks
    15
    Thanked 1 Time in 1 Post

    Question php if variable is empty default to other text

    I am wondering if it is possible to default to a piece of text if a varable is empty in PHP?

    Something like this, maybe????

    PHP Code:
    <?php echo stripslashes($row["moreinfo"]) || "None."?>
    Picco

  • #2
    Regular Coder
    Join Date
    Nov 2005
    Location
    North Canton, Ohio
    Posts
    118
    Thanks
    11
    Thanked 4 Times in 4 Posts
    PHP Code:
    <?php
    if (!isset($row["moreinfo"]) ||  $row["moreinfo"]==NULL)
    {
    $row["moreinfo"]="None.";
    }
    echo 
    stripslashes($row["moreinfo"]);
    ?>
    If you know the variable will always be set no matter what, you don't need the !isset($row["moreinfo"]).
    Last edited by Blaher; 02-24-2008 at 03:56 PM.

  • #3
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,096
    Thanks
    15
    Thanked 1 Time in 1 Post

  • #4
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    Quote Originally Posted by Blaher View Post
    PHP Code:
    <?php
    if (!isset($row["moreinfo"]) ||  $row["moreinfo"]==NULL)
    {
    $row["moreinfo"]="None.";
    }
    echo 
    stripslashes($row["moreinfo"]);
    ?>
    If you know the variable will always be set no matter what, you don't need the !isset($row["moreinfo"]).
    Or even better:

    PHP Code:
    if (empty($row['moreinfo'])) {
       
    $row['moreinfo'] = 'None.';
    }
    echo 
    stripslashes($row['moreinfo']); 

  • #5
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,096
    Thanks
    15
    Thanked 1 Time in 1 Post
    I was thinking more using a ternary operator to do this.....similarly to what is done in Perl....

  • #6
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Quote Originally Posted by crmpicco View Post
    I was thinking more using a ternary operator to do this.....similarly to what is done in Perl....
    PHP Code:
    $row['moreinfo'] = (empty($row['moreinfo'])) ? 'None.' $row['moreinfo'];
    echo 
    stripslashes($row['moreinfo']; 


    Sidenote: Hey JohnDubya....congrats on becoming a mod.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #7
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    Quote Originally Posted by StupidRalph View Post
    Sidenote: Hey JohnDubya....congrats on becoming a mod.
    Thanks!


  •  

    Posting Permissions

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