• Home
  • PHP
  • MySQL
  • Laravel
  • Demos
  • HTML
  • jQuery
  • Framework
  • Request Tutorial
PHP Lift
  • Home
  • Demos
  • Advertisement
PHP Lift
  • Home
  • PHP
  • MySQL
  • Laravel
  • Demos
  • HTML
  • jQuery
  • Framework
  • Request Tutorial
  • Follow
    • Facebook
    • Twitter
    • Google+
    • Pinterest
    • Youtube
    • Instagram
    • RSS
Examining variable types in PHP
Home
PHP

Examining variable types in PHP

September 15th, 2025 Ivan Dimov PHP 0 comments

Facebook Twitter Google+ LinkedIn Pinterest

In PHP, there are a range of built-in functions which you can call to check variable types.  They return a Boolean value  – true if the variable is of that type and false if it is not.

To check if a variable contains a string, you can use the is_string function. You can use it like that:

echo var_dump(is_string("DQDQ")); // true



echo var_dump(is_string(23)); // false



echo var_dump(is_string("11")); // true

You should be aware that a number within a string (for example, coming from user input) is still a string and not a number when checking the variable type.

To check whether a variable is an integer, you can use the is_int function.

echo var_dump(is_int("11")); //false



echo var_dump(is_int(11)); //true

The point here is that strings with integers in them will result in a falsey return value.

To simply check whether a variable holds a numeric value, you can use the is_numeric function.

You can simply check if the variable holds a numeric value, no matter of the data type with the help of the is_numeric function.

echo var_dump(is_numeric("11")); // true



echo var_dump(is_numeric(11)); // true



echo var_dump(is_numeric("dqdq")); // false



echo var_dump(is_numeric("1123.23")); // true

The function will return true if the value contains an integer or a float, regardless of whether it is a string or of a numeric data type.

To check if a variable contains a floating point number, you can use the is_float method.

echo var_dump(is_float("11")); // false



echo var_dump(is_float(11.23)); // true



echo var_dump(is_numeric("word")); // false



echo var_dump(is_float("1123.23")); // false

The function will return true only if the variable contains a floating point number, strings excluded.

To check if the variable contains an array, you can use the is_array($var) function.

Similarly, to check if a variable is an object, you can use the is_object function. Here is a simple example:

<?php



class myClass {



                public function __construct($age) {



                $this->age = 26;



                }



}



$myLife = new myClass(26);



echo var_dump(is_object($myLife));

You can also use the is_bool function to check for Booleans, it checks for the data type so a string holding a Boolean would not count.

echo var_dump(is_bool("true")); //false



echo var_dump(is_bool(false)); //true

You can also use the class_exists function to check if a class exists:

class myClass {



                public function __construct($age) {



                $this->age = 26;



                }



}



echo var_dump(class_exists("myClass"));

You can give it a string with the expected name of the class.

You can use the instance of operator check if a variable contains an instance of a given class. Here is an example:

<?php



class myClass {



                public function __construct($age) {



                $this->age = $age;



                }



}



$me = new myClass(26);



echo var_dump($me instanceof myClass);

 

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook

Related

  • Tags
  • PHP
  • Variables
Facebook Twitter Google+ LinkedIn Pinterest
Next article Const vs. Let vs. Var in Javascript. Which one should you use?
Previous article 10 Best Food Delivery Apps Systems for Small Restaurants

Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter

Related Posts

Understanding the Factory Design Pattern in PHP: Simplifying Object Creation PHP
September 18th, 2025

Understanding the Factory Design Pattern in PHP: Simplifying Object Creation

Choosing the Right PHP Framework for Your Web Application: A Comprehensive Guide Framework
September 18th, 2025

Choosing the Right PHP Framework for Your Web Application: A Comprehensive Guide

Is PHP dead in 2021? Is PHP still relevant or worth the effort? PHP
September 17th, 2025

Is PHP dead in 2021? Is PHP still relevant or worth the effort?

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe
Get new posts by email:
Powered by follow.it
Advertisement
Like us
Recent Posts
  • How do I send an auto-reply message on Skype?
  • Understanding the Factory Design Pattern in PHP: Simplifying Object Creation
  • Choosing the Right PHP Framework for Your Web Application: A Comprehensive Guide
  • Top 7 Websites To Get Your First Paid Internship
  • Laravel Data and Value Objects: Harnessing the Power of Immutability and Consistency
Categories
  • API
  • Bootstrap
  • Bot
  • CSS
  • CSS 3
  • Database
  • Designing
  • Framework
  • Guide
  • HTML
  • HTML 5
  • JavaScript
  • jQuery
  • Laravel
  • MySQL
  • Node.js
  • oAuth
  • Payment
  • PHP
  • Python
  • Social
  • Tips
  • Web 3.0
  • WordPress
Weekly Tags
  • PHP
  • javascript
  • How to
  • laravel
  • css
  • HTML to PDF
  • jQuery
  • api
  • Web Development
  • MYSQL
  • About
  • Privacy Policy
  • Back to top
© PHPLift.net. All rights reserved.