Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

PHP

How do you determine the data type of a variable in PHP?

PHP is more tolerant than other programming languages and does not require a fixed data type to be specified for a variable. In fact, there is not even an opportunity to do so. Nevertheless, the value of each variable has a data type that influences the processing of this value. For example, adding integers behaves differently than adding strings.
 

gettype()

To find out what data type the value of a variable has, gettype($var) can be used. This function returns one of the following string, which correspond to the known data types in PHP: ‘boolean’, ‘integer’, ‘double’, ‘string’, ‘array’, ‘object’, ‘resource’, ‘NULL’, ‘unknown type’. Floats are represented by ‘double’.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
function func($par) {
echo("Data type of '".$par."' is '".gettype($par)."'.\n");
}
func(50);
func(2.5);
func("hello");
func(null);
?>
<?php function func($par) { echo("Data type of '".$par."' is '".gettype($par)."'.\n"); } func(50); func(2.5); func("hello"); func(null); ?>
<?php
    function func($par) {
        echo("Data type of '".$par."' is '".gettype($par)."'.\n");
    }
     
    func(50);
    func(2.5);
    func("hello");
    func(null);
?>

Output:

Data type of '50' is 'integer'.
Data type of '2.5' is 'double'.
Data type of 'hello' is 'string'.
Data type of '' is 'NULL'.

 
PHP MCQ - Multiple Choice Questions and Answers

is_* functions

The function gettype() should only be used to output the datatype of a variable. Testing for special datatypes – with this function – is not recommended, because the returns may change at some point. Instead, the functions is_boolean($var), is_string($var), is_float($var), is_array($var), is_object($var), is_resource($var) and is_null($var) should be used.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
function square($val)
{
// Only integers or floats should be squared by this function
// an error is thrown for all other data types.
if (!is_int($val) && !is_float($val))
{
throw new Exception('Invalid parameter passed. '
.'Integer was expected, '.gettype($val).' was given.');
}
return ($val * $val);
}
// square with 2 as a number
var_dump(square(2));
// square with 2 as a string, produces an error
try
{
var_dump(square("2"));
}
catch (Exception $e)
{
echo($e->getMessage());
}
?>
<?php function square($val) { // Only integers or floats should be squared by this function // an error is thrown for all other data types. if (!is_int($val) && !is_float($val)) { throw new Exception('Invalid parameter passed. ' .'Integer was expected, '.gettype($val).' was given.'); } return ($val * $val); } // square with 2 as a number var_dump(square(2)); // square with 2 as a string, produces an error try { var_dump(square("2")); } catch (Exception $e) { echo($e->getMessage()); } ?>
<?php
    function square($val) 
    {
        // Only integers or floats should be squared by this function
        // an error is thrown for all other data types.
        if (!is_int($val) && !is_float($val)) 
        {
            throw new Exception('Invalid parameter passed. '
                .'Integer was expected, '.gettype($val).' was given.');
        }
        return ($val * $val);
    }
 
    // square with 2 as a number
    var_dump(square(2));
 
    // square with 2 as a string, produces an error
    try 
    {
        var_dump(square("2"));
    } 
    catch (Exception $e) 
    {
        echo($e->getMessage());
    }
?>

Output:

int(4)
Invalid parameter passed. Integer was expected, string was given.
mcq

Leave a Reply

Your email address will not be published. Required fields are marked *