stupid javascript question Please help...

Discussion in 'JavaScript' started by JEET, Feb 26, 2006.

  1. #1
    Hi,
    This is probably a stupid question but I don't know much of these languages.

    I have a variable named "myname"

    I want to check if the variable is empty .
    If yes then I have to write the following:
    var myname= "jeet";
    If no then do nothing .

    What will be the code for this ?
    Actually a function is sending a value to this variable but if the value is an empty string then I want to have "jeet" in it .

    Thank you
    jeet
     
    JEET, Feb 26, 2006 IP
  2. kk5st

    kk5st Prominent Member

    Messages:
    3,497
    Likes Received:
    376
    Best Answers:
    29
    Trophy Points:
    335
    #2
    
    if (! myname) {
       myname="jeet";
    }
    Code (markup):
    That ought to work if the variable truly contains a null value.

    cheers,

    gary
     
    kk5st, Feb 26, 2006 IP
  3. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #3
    Hi,
    That's not working ...
    I think there is another bug in the script I am writing.
    I might not be able to do this using javascript.
    Later in the code I am putting this value in a PHP variable named $myn but that's not empty.
    It's something like this :
    $myn= "my name is ".$c;

    So here's what I tried .
    code:
    <?php
    $m = "my name is ";
    // comparing $myn to $m
    if $myn = $m
    {
    $myn= "myname is jeet";
    }
    ?>

    But that keeps giving me a parssing error.

    I am checking if the value of $myn is the same as $m and if yes then $myn is set to a complete string specified by me else nothing is done.
    Can you suggest something here also?

    Thank you
    jeet
     
    JEET, Feb 27, 2006 IP
  4. kk5st

    kk5st Prominent Member

    Messages:
    3,497
    Likes Received:
    376
    Best Answers:
    29
    Trophy Points:
    335
    #4
    For one thing, if $myn = $m would always evaluate as true if you had remembered the (). Do, if ($myn == $m).

    In this case, you should do two tests;
    
    if (empty($myn)) {
       echo "Houston, we have a problem.";
    }
    else if ($myn == $m) {
       $myn = $m . " Jeet";
    }
    Code (markup):
    Why not test $c?

    cheers,

    gary
     
    kk5st, Feb 27, 2006 IP
  5. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #5
    Excellent Brilliant Superb!!!
    Very cool !
    I was struggling with this since last evening . It's been like 24 hours now...

    Thank you a million times for the help!
    Best regards
    jeet
     
    JEET, Feb 27, 2006 IP