payment by upi: sinhamit@icici or payment by bank account name: amit kumar sinha, account number: 2646728782 IFSC code: KKBK0005660 SWIFT: KKBKINBB

Please support if you like my work by payment through upi: sinhamit@icici or payment by bank

account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB


Variable handling Functions   in Category: PHP   by amit

🕙 Posted on 2023-05-24 at 21:04:01     Read in Hindi ...


Checking Data-Type & Values

is_numeric(), is_null()

    is_numeric() function checks whether a literal or a variable/constant name or output of an expression is numeric data, that is, whether it is integer or floating point number. Thus, PHP_INT_MAX, PHP_INT_MIN, PHP_FLOAT_MAX, PHP_FLOAT_MIN, NAN, M_PI, pi(), INF, -INF, etc. are all numeric data-type.

<?php var_dump( is_numeric( pi() ) ); // outputs bool(true) ?><br />

<?php var_dump( is_numeric( "12.5" ) ); // Outputs bool(true) ?><br />

    is_null() function checks whether a literal or a variable/constant name or output of an expression is NULL. In following example, the variable name $not_var is not declared, and the output is bool(true) along with ERROR shown as below:

<?php var_dump( is_null( NAN ) ); // Outputs bool(false) ?><br />

<?php var_dump( is_null( $not_var ) ); // Outputs ERROR as shown below: ?><br />


Warning: Undefined variable $not_var in C:\xampp\htdocs\php_project\phptest.php on line 1
bool(true)

is_infinite(), is_finite(), is_nan()

    is_infinite() and is_finite() are Math functions which check whether the value of a literal or a variable/constant name or output of an expression is equal to infinity or not. is_nan() is also a Math function, which checks whether the value of a literal or a variable/constant name or output of an expression is NAN (not a number).

<?php var_dump( is_infinite( log( -1 ) ) ); // log(-1) is float(NAN), therefore outputs bool(false) ?><br />

<?php var_dump( is_infinite( fdiv( PHP_INT_MIN, 0 ) ) ); // division by 0 returns infinity, therefore outputs bool(true) ?><br />

<?php var_dump( is_finite( -INF ) ); // -INF is negative infinity, therefore outputs bool(false) ?><br />

<?php var_dump( is_finite( 3**(1/3) ) ); // cube-root of 3 is a irrational number, and not infinity, therefore outputs bool(true) ?><br />

is_string(), is_scalar()

    is_string() function checks whether a literal or a variable/constant name or output of an expression is string data-type. Even a numeric string is a string data-type. is_scalar() function checks whether a literal or a variable/constant name or output of an expression is either boolean, integer, float or string data-type, the output will be bool(true). All other data-types, for example, array, NULL, object, resource, etc. are not scalar data-types, and these will output bool(false).

<?php var_dump( is_string( "12.5" ) ); // outputs bool(true) ?><br />

<?php var_dump( is_scalar( [] ) ); // outputs bool(false) ?><br />

<?php var_dump( is_scalar( NULL ) ); // outputs bool(false) ?><br />

is_array(), in_array()

    is_array() function checks whether a literal or a variable/constant name or output of an expression is array data-type. in_array() function checks whether a literal or a variable/constant name or output of an expression is present as an item in an array. in_array() function also outputs boolean value, and it takes two arguments (third argument is optional, checks data-type of item): in_array(mixed $needle, array $haystack, bool $strict = false): bool

<?php var_dump( is_array( [] ) ); // outputs bool(true) ?><br />

<?php var_dump( in_array( "1", [ 0, 1, 2, 3 ] ) ); // outputs bool(true) ?><br />

<?php var_dump( in_array( "1", [ 0, 1, 2, 3 ], true ) ); // outputs bool(false) ?><br />

Variable handling Functions

    Following pre-defined functions can be applied directly on variables to get results. Some of following pre-defined functions are already explained, and other important functions will be explained later, whenever they are required.

boolval function is equivalent to (bool) type-casting operation.

intval function is equivalent to (int) type-casting operation.

floatval or doubleval function is equivalent to (flaot) type-casting operation.

strval function is equivalent to (string) type-casting operation.

  • boolval − Get the boolean value of a variable
  • debug_zval_dump − Dumps a string representation of an internal zval structure to output
  • doubleval − Alias of floatval
  • empty − Determine whether a variable is empty
  • floatval − Get float value of a variable
  • get_debug_type − Gets the type name of a variable in a way that is suitable for debugging
  • get_defined_vars − Returns an array of all defined variables
  • get_resource_id − Returns an integer identifier for the given resource
  • get_resource_type − Returns the resource type
  • gettype − Get the type of a variable
  • intval − Get the integer value of a variable
  • is_array − Finds whether a variable is an array
  • is_bool − Finds out whether a variable is a boolean
  • is_callable − Verify that a value can be called as a function from the current scope.
  • is_countable − Verify that the contents of a variable is a countable value
  • is_double − Alias of is_float
  • is_float − Finds whether the type of a variable is float
  • is_int − Find whether the type of a variable is integer
  • is_integer − Alias of is_int
  • is_iterable − Verify that the contents of a variable is an iterable value
  • is_long − Alias of is_int
  • is_null − Finds whether a variable is null
  • is_numeric − Finds whether a variable is a number or a numeric string
  • is_object − Finds whether a variable is an object
  • is_real − Alias of is_float
  • is_resource − Finds whether a variable is a resource
  • is_scalar − Finds whether a variable is a scalar
  • is_string − Find whether the type of a variable is string
  • isset − Determine if a variable is declared and is different than null
  • print_r − Prints human-readable information about a variable
  • serialize − Generates a storable representation of a value
  • settype − Set the type of a variable
  • strval − Get string value of a variable
  • unserialize − Creates a PHP value from a stored representation
  • unset − Unset a given variable
  • var_dump − Dumps information about a variable
  • var_export − Outputs or returns a parsable string representation of a variable

Leave a Comment: