Although it has nothing to do with this particular function, this is how I do alternating row classes. I haven't used it on large tables so I don't know about speed but it doesn't rely on numbers so you can name them whatever you want:
<?
$altRowClasses = array('row1', 'row2', 'rowYermom');
echo '<table>';
for ($i = 0; i<15; i++)
{
$rowClass = array_shift($altRowClasses);
echo '<tr class="' . $rowClass . '">';
echo '<td>table_data</td>';
echo '</tr>';
array_push($altRowClasses, $rowClass);
}
echo '</table>';
?>
is_int
(PHP 4, PHP 5)
is_int — 変数が整数かどうかを検査する
説明
bool is_int ( mixed $var )与えられた変数が整数かどうかを検査します。
注意: 変数が数値もしくは数値文字列の場合 (フォームからの入力の場合は 常に文字列となります) 、is_numeric() を使用する必要があります。
パラメータ
- var
評価する変数
返り値
もし var が 整数型 の場合 TRUE、 そうでない場合は FALSE を返します。
参考
| is_bool() |
| is_float() |
| is_numeric() |
| is_string() |
| is_array() |
| is_object() |
is_int
fraug
29-Aug-2007 01:17
29-Aug-2007 01:17
matt at mattkemp dot co dot uk
18-Aug-2007 04:53
18-Aug-2007 04:53
I've found a much better way of making table rows is something like this:
<?php
echo '<table>';
for ($i = 0; i<15; i++)
{
echo '<tr class="row'.($i%2).'">';
echo '<td>table_data</td>';
echo '</tr>';
}
echo '</table>';
*/ If youre not using CSS, then just swap in the if: */
echo '<table>';
for ($i = 0; i<15; i++)
{
if (i%2 == 0) echo '<tr bgcolor="aqua">';
else echo '<tr bgcolor="white">';
echo '<td>table_data</td>';
echo '</tr>';
}
echo '</table>';
?>
Using modulo arithmetic should be faster than calling is_int() with every table row. Every little helps when you have 12,000 line table ;)
support at alleypets dot com
06-Jul-2007 11:29
06-Jul-2007 11:29
is_int can make alternating table rows. See:
<?php
print "<table border=1 rules=rows><tr><th>Header 1:</th><th>Header 2:</th></tr>";
for($i=0;$i<=12;$i++) {
if(is_int($i/2)) {
print "<tr bgcolor='aqua'> <td>text or var 1</td><td>text or var 2</td></tr>";
}
else {
print "<tr bgcolor='white'> <td>text or var 3</td><td>text or var 4</td></tr>";
}
}
print "</table>";
?>
tudor at tudorholton dot com
05-Jul-2007 10:27
05-Jul-2007 10:27
Please note this from the Integer datatype page:
"The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). PHP does not support unsigned integers. Integer size can be determined from PHP_INT_SIZE, maximum value from PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5."
This is particularly important if you are doing validation of large keys or any number larger than 2,000,000,000 (e.g. telephone numbers)
Ender at soldat dot nl
16-Feb-2006 08:38
16-Feb-2006 08:38
Be aware that is_numeric (mentioned in this article as the proper way to validate string numbers) also allows numbers in scientific and hexadecimal annotation. Thus DO NOT USE that function to validate user input that will be used as id number for in a query for example, this could cause mysql errors. Use ctype_digit instead.
ludvig dot ericson at gmail dot com
07-Jan-2006 06:00
07-Jan-2006 06:00
I would like to say that is_int() is pretty helpfull when looking for neat proper ways to check functions that return either integers or booleans (false) on failure (strpos, socket_select, etc.)
<?php
function mySelect() {
global $someSockets;
$ret = socket_select($someSockets, $o = array(), $e = array(), 0);
if (!$ret)
return is_int($ret);
/* FURTHER PROCESSING HERE */
return true; // Return true if the function proceeded as expected.
}
?>
The point of doing this is that if you put this in a while() loopo, you'll break it when the select fails.
<?php
while (mySelect());
?>
Hope you get the point
- toxik
lclkk at urbanvagabond dot net
16-Sep-2003 11:24
16-Sep-2003 11:24
I think the function below is a robust test for integers working on all datatypes. It works by first checking that a number can be evaluated numerically, and then secondly that the integer evaluation matches the original number.
Test cases are included.
<?
function myIsInt ($x) {
return (is_numeric($x) ? intval($x) == $x : false);
}
function Test($x) {
echo "$x is " . ( myIsInt($x) ? ('an integer. The integer value is ' . intval($x)) : 'not an integer.');
echo "\n";
}
echo "These should be integers...\n";
Test(1);
Test(5);
Test(10);
Test(10.0);
Test(20.0);
Test(-20.0);
Test(0+4+4.5+4.5);
Test("10.0");
Test("+14");
Test("-15");
Test("0");
echo "\nThese should not be integers...\n";
Test(true); // watch out, this displays as '1'
Test(false);
Test("moose");
Test("3.5");
Test("-214235.5");
Test(""); // empty string
Test(array(1,2,3));
Test(dir('.')); // object
Test(null);
?>
gabe at websaviour dot com
15-Jul-2003 03:08
15-Jul-2003 03:08
Although this can be inferred from the documentation, beware of numeric strings. I ran into the problem in a MySQL app where I would either SELECT an INT PRIMARY KEY or INSERT a new record and use mysql_insert_id() to get the KEY before continuing onto the new section.
I used is_int() to make sure the subsequent queries wouldn't break when using the key variable. Unfortunately I failed to realize that while mysql_insert_id() returns an int, mysql_result() always returns a string even if you are SELECTing from an INT field.
Spent at least 30 minutes trying to figure out why existing records weren't getting linked, but new records would link fine. I ended up using intval() on mysql_result() to make sure subsequent queries still always work.
phpguru at gmx dot ch
06-Jul-2003 12:50
06-Jul-2003 12:50
To Logan:
There's also a simple non-regexp way to convert a (form) value into an integer if it consists of numbers only - although with a trap (see below):
if ($_POST["number"] == (int)$_POST["number"]) $_POST["number"] = (int)$_POST["number"];
The "traps" (or "side effects") appear with values like "" (empty string) and false (boolean), which are converted to 0 (integer). But in certain cases this might be desirable or/and usefull ;-)
Solutions like
if (($_POST["number"] + 1 - 1) == $_POST["number"]) ...
falls into the same category.
mark at g33kz dot co dot uk
18-Jun-2003 09:44
18-Jun-2003 09:44
Or you could just use is_numeric()
I have a file called input.php which I run at the beginning of all my scripts which makes sure all my input numbers are converted to integers automatically.
if ($_GET) {
foreach ($_GET as $k => $v) {
$_GET[$k] = trim (stripslashes ($v));
if (is_numeric ($v)) {
$_GET[$k] = intval ($v);
}
}
}
if ($_POST) {
foreach ($_POST as $k => $v) {
$_POST[$k] = trim (stripslashes ($v));
if (is_numeric ($v)) {
$_POST[$k] = intval ($v);
}
}
}
if ($_COOKIE) {
foreach ($_COOKIE as $k => $v) {
$_COOKIE[$k] = trim (stripslashes ($v));
if (is_numeric ($v)) {
$_COOKIE[$k] = intval ($v);
}
}
}
logan at logannet dot net
11-Feb-2003 08:42
11-Feb-2003 08:42
[[Editors note: Or you can simply use is_numeric()]]
Some people have offered their ways to find out if a string from a form is an integer or not, here's my way:
if(ereg("^[0-9]+$", $_POST["number"])) $_POST["number"] = (int)$_POST["number"];
In psuedo code:
if you are a string full of numbers then convert yourself to an integer
So instead of just checking if its a string full of numbers you check and then convert it, which means you can use the standard is_int. You can also do:
if(ereg("^[0-9]+$", $_POST["number"])) $_POST["number"] += 0;
I think the first way i mentioned is better because your coding what you want to do, rather than the second way that uses a side effect of adding 0 to convert the string.
The first way also may make your code ever so slightly faster (nothing noticeable) as php does not need to add 0 to the number after it converts it.
Also note an integer is full numbers (1, 2, 3 etc) not decimal numbers (1.1, 2.4, 3.7 etc), to convert decimal numbers you could use something like:
if(ereg("^[.0-9]+$", $_POST["number"])) $_POST["number"] = (float)$_POST["number"];
OR
if(ereg("^[.0-9]+$", $_POST["number"])) $_POST["number"] += 0;
But note that these would not work with is_int(), because they are not integers.