Help to convert a string variable to an integer one!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Help to convert a string variable to an integer one!

Post by dimitris »

I have this code at the beggining of my script. The problem is that i want to use $prints as an integer not as a string variable.

Code: Select all

<?php
if(isset($number_of_rows))
				{
				$prints=$number_of_rows;
				}
				else
				{
				$prints=100;
			    }
				
				
				if (is_int($prints)){
		  echo' IT IS AN INTEGER';
		  }
		  if(is_string($prints)){
		  echo' IT IS A STRING';
		  echo $prints;
		  } 
?>
If i have entered $number_of_rows it comes from a html form:

Code: Select all

&lt;form name="select_number_of_rows" method="post" action="view_customer_info_grid.php?step='.$cstep.'"&gt;
&lt;input name="cstep" type="hidden" class="style2" id="go" value="'.$step.'"&gt;
&lt;span class="style2"&gt;Number of results on each page:&lt;/span&gt;	  &lt;select name="number_of_rows" class="style2" id="number_of_rows"&gt;
	    &lt;option selected&gt;100&lt;/option&gt;
	    &lt;option&gt;50&lt;/option&gt;
	    &lt;option&gt;20&lt;/option&gt;
	    &lt;option&gt;10&lt;/option&gt;
	    &lt;/select&gt;
		&lt;input name="go" type="submit" class="style2" id="go" value="Go"&gt;
&lt;/form&gt;
The script produces IT IS AN INTEGER100 if i run my page at first time but it becomes IT IS STRING20 if choose 20 from the menu!

Any ideas to make the convertion back to integer ??? :P
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

[php_man]intval[/php_man]($number_of_rows)
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Post by dimitris »

Weirdan wrote:[php_man]intval[/php_man]($number_of_rows)
Despite it is now an INTEGER with value 20 i have this error in one of the lines:

Fatal error: Unsupported operand types

I try to execute this :

Code: Select all

<?php
if($z-$prints>0)
		  {$min=$z-$prints;}else{$min=0;}
		  
			 for( $i=$z;$i>$min;$i-- )
			 {
....
?>
$z is an integer taken as postdata
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Are you sure? I've been able to reproduce 'Unsupported operand types' with arrays and objects only; it works perfectly with strings and numbers...
you can var_dump($variable) to see it's real type before if condition.
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Post by dimitris »

Weirdan wrote:Are you sure? I've been able to reproduce 'Unsupported operand types' with arrays and objects only; it works perfectly with strings and numbers...
you can var_dump($variable) to see it's real type before if condition.
I converted $z as well using intval() and now the script works fine! :D Thanks a lot!
Post Reply