Variable related stuff

Discussion in 'PHP' started by Fracisc, Nov 27, 2008.

  1. #1
    I need this:

    $variable = if variable has a value echo it, else echo default value.

    How can I code that in php?
     
    Fracisc, Nov 27, 2008 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    echo (strlen($variable) != 0) ? 'default' : $variable;

    that's all :)
     
    EricBruggema, Nov 28, 2008 IP
  3. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Shouldn't it be another way round?

    echo (strlen($variable) == 0) ? 'default' : $variable;
    PHP:
    or simply

    echo ($variable == '') ? 'default' : $variable;
    PHP:
     
    ads2help, Nov 28, 2008 IP
  4. djzmo

    djzmo Active Member

    Messages:
    165
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #4
    
    echo (!$variable) ? 'default' : $variable;
    
    PHP:
     
    djzmo, Nov 28, 2008 IP
  5. Fracisc

    Fracisc Well-Known Member

    Messages:
    3,670
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    195
    #5
    OK, I know that, but how do I set it in a URL?

    Include http://site.com?var=[get var or default]&...
     
    Fracisc, Nov 28, 2008 IP
  6. garrettheel

    garrettheel Peon

    Messages:
    341
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    To get a variable from a url, just use the following example, pretending that the URL is mysite.com?var=somevar

    
    $var = $_GET['var'];
    echo $var;
    
    Code (markup):
    That code would echo "somevar" on the page. Also remember to sanitize any input before doing anything with it in the script.
     
    garrettheel, Nov 28, 2008 IP
  7. mrmaf

    mrmaf Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Another way to check whether variable is empty or not

    $name="adeel fakhar";

    if(empty($name))
    {
    echo "variable is empty";
    }
    else{
    echo $name;
    }
     
    mrmaf, Nov 29, 2008 IP
  8. mrmaf

    mrmaf Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Dear Fracisc you can set any variable in url like this
    <?php
    $name="Adeel Fakhar";
    ?>

    <a href="http://site.com?name=<?php echo $name;?">Click Here</a>
     
    mrmaf, Nov 29, 2008 IP