Showing posts with label PHP is_string. Show all posts
Showing posts with label PHP is_string. Show all posts

Friday, 26 June 2015

PHP: Primitive Types and Checking Functions

Type determines the way that data can be managed in your scripts. You use the string type to display character data, for example, and manipulate such data with string functions. Integers are used in mathematical expressions; Booleans are used in test expressions, and so on. These categories are known as primitive types.
Primitive Types and Checking Functions in PHP
Type Checking Function     Type     Description
is_bool()     Boolean     One of the two special values true or false
is_integer()     Integer     A whole number
is_double()     Double     A floating point number (a number with a decimal point)
is_string()     String     Character data
is_object()     Object     An object
is_array()     Array     An array
is_resource()     Resource     A handle for identifying and working with external resources such as databases or files
is_null()     Null     An unassigned value

Checking the type of a variable can be particularly important when you work with method and
function arguments.

Wednesday, 3 June 2015

Monday, 23 February 2015

PHP: Highlight specific words in a phrase

Sometimes, for example, when displaying search results, it is a great idea to highlight specific words. This is exactly what the following function can do:

<?php

function highlight($sString, $aWords) {

 if (!is_array ($aWords) || empty ($aWords) || !is_string ($sString)) {
  return false;
 }

 $sWords = implode ('|', $aWords);
  return preg_replace ('@\b('.$sWords.')\b@si', '<strong style="background-color:yellow">$1</strong>', $sString);
}
?>

Friday, 3 October 2014

is_string in PHP

is_string — Find whether the type of a variable is string

Syntax: 
bool is_string ( mixed $var )

Finds whether the type given variable is string.
Parameters: 

var

    The variable being evaluated.

Return Values:

Returns TRUE if var is of type string, FALSE otherwise.
Examples:

Example #1 is_string() example
<?php
$values = array(false, true, null, 'abc', '23', 23, '23.5', 23.5, '', ' ', '0', 0);
foreach ($values as $value) {
    echo "is_string(";
    var_export($value);
    echo ") = ";
    echo var_dump(is_string($value));
}
?>

The above example will output:

is_string(false) = bool(false)
is_string(true) = bool(false)
is_string(NULL) = bool(false)
is_string('abc') = bool(true)
is_string('23') = bool(true)
is_string(23) = bool(false)
is_string('23.5') = bool(true)
is_string(23.5) = bool(false)
is_string('') = bool(true)
is_string(' ') = bool(true)
is_string('0') = bool(true)
is_string(0) = bool(false)