what's "int()" of ASP in PHP?

Discussion in 'PHP' started by gilgalbiblewheel, Nov 21, 2007.

  1. #1
    What's wrong with this? I tried to translate it from ASP to PHP.
        echo '<option ';
    	if int(letter_id) = int($row['letter_id']){
    	echo 'selected';
    	}
    	echo ' value="'.$row['letter_id'].'"> Spoke '.$row['letter_id'].'</option>'."\n";
    PHP:
    In ASP it would've been:
    <option <%if int(letter_id) = int(RS("letter_id")) then response.write "selected" end if%> value="<%=RS("letter_id")%>">SPOKE <%=RS("letter_id")%></option>
    Code (markup):

     
    gilgalbiblewheel, Nov 21, 2007 IP
  2. rhythmiccycle

    rhythmiccycle Guest

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I convert to integers like this:

    $Number = 12.345;
    $newNumber = (int)$Number;
    echo "before: ". $Number;
    echo"<br>after: ". $newNumber;

    this will display:

    before: 12.345
    after: 12

    Let me know if that helps
     
    rhythmiccycle, Nov 21, 2007 IP
  3. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    That's not what intended. I have a database table where I'm retrieving the values into a dropdown. By then selecting from one of the dropdown selection it should write selected next to it.
     
    gilgalbiblewheel, Nov 21, 2007 IP
  4. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #4
    This solved:
        echo '<option ';
    	if (intval($letter_id) == intval($row['letter_id'])){
    	echo 'selected';
    	}
    PHP:
     
    gilgalbiblewheel, Nov 21, 2007 IP
  5. thenetninja

    thenetninja Peon

    Messages:
    314
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    first up, the php if statement has to wrapped in ( )'s
    i.e if($number!='1'){ echo 'not a number 1'; }else{ echo 'this is a number 1'; }

    $sel=1;
    echo '<option ';
    if($sel==1 && intval($sel)){
    echo 'selected';
    }
    echo ' value="'.$sel.'" yadda yadda yadda';

    have fun with that one
     
    thenetninja, Nov 21, 2007 IP
  6. thenetninja

    thenetninja Peon

    Messages:
    314
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    lol, you got it =)
     
    thenetninja, Nov 21, 2007 IP