Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP: int - char/string conversion and vice versa

Options
  • 02-04-2002 3:20pm
    #1
    Registered Users Posts: 14,148 ✭✭✭✭


    Hey guys,

    just wondering if anyone knows how to convert a string to int (or vice versa) in PHP. I can't seem to find anything in the manual (before someone utters the immortal acrynom :p)

    What I have is this:

    $today = getdate() ;
    $var1 = 2000 ; // start year
    $var2 = $var1++ ;
    $var3 = $today ; // current year (taken from system clock)

    ...
    ...
    ...

    echo $var1 ;
    while($var2 < $var3)
    {
    echo $var2
    $var2++ ;
    }
    echo $var3 ;

    basically what I'm trying to do is create an input field (<select>) which is self-updating as the years roll on so to speak.

    The problem is (best as I can make it) that $var3 isn't an integer and that's the stumbling block. So any suggestions would be greatly appreciated :)

    [edit]
    Where's C/C++'s itoa or atoi functions when you need them?!!! :ermm:
    [/edit]


Comments

  • Registered Users Posts: 944 ✭✭✭nahdoic


    [PHP]<?php
    $today = getdate();
    $var1 = 2000 ; // start year
    $var2 = $var1++ ;
    $var3 = $today ; // current year (taken from system clock)


    echo $var1 ;
    while($var2 < $var3)
    {
    echo $var2 ;
    $var2++ ;
    }
    echo $var3 ;

    ?> [/PHP]


  • Banned (with Prison Access) Posts: 16,659 ✭✭✭✭dahamsta


    See the section on Type Casting on the Type Juggling page. Also see the intval() and related functions. In the manual. :)

    adam


  • Registered Users Posts: 14,148 ✭✭✭✭Lemming


    Originally posted by dahamsta
    See the section on Type Casting on the Type Juggling page. Also see the intval() and related functions. In the manual. :)

    adam

    You, sir, are a legend!!! :D
    I'll even forgive your manual reference :p (I kinda suspected I was lookin in the wrong places mind you! :rolleyes: )


  • Registered Users Posts: 944 ✭✭✭nahdoic


    Gonna stop trying to answer these programming questions. Never seem to understand what people want. Anyway here is another solution too.

    [php]<?php
    $today = getdate() ;
    $var1 = 2000 ; // start year
    $var2 = $today ; // current year (taken from system clock)
    for($i=$var1;$i<=$var2;$i++)
    echo $i;
    ?>
    [/php]


Advertisement