Page 1 of 1

Help to convert a string variable to an integer one!

Posted: Wed Jul 28, 2004 7:13 am
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

Posted: Wed Jul 28, 2004 7:35 am
by Weirdan
[php_man]intval[/php_man]($number_of_rows)

Posted: Wed Jul 28, 2004 8:08 am
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

Posted: Wed Jul 28, 2004 9:57 am
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.

Posted: Thu Jul 29, 2004 5:34 am
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!