• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / PHP / php is_string() Function – Check if Variable is String in php

php is_string() Function – Check if Variable is String in php

April 6, 2022 Leave a Comment

The php is_string() function checks if a variable is an string or not. If the given variable is an string, is_string() returns TRUE. Otherwise, is_string() returns FALSE.

$string = "This is a string";
$array = array(1,2,3);
$float = 1.0;

var_dump(is_string($string));
var_dump(is_string($array));
var_dump(is_string($float));

// Output:
bool(true)
bool(false)
bool(false)

When working with different variables in your php programs, the ability to easily check and know if a variable is of a certain type is very valuable.

One such case is when you want to know if a variable is a string. The php is_string() function allows us to check if a variable is a string.

To use is_string(), just pass the variable you want to check. If the given variable is a string, is_array() returns TRUE. Otherwise, is_string() returns FALSE.

Below are a few examples of using the is_string() to find out if a variable is a string or not in php.

$string = "This is a string";
$array = array(1,2,3);
$float = 1.0;

var_dump(is_string($string));
var_dump(is_string($array));
var_dump(is_string($float));

// Output:
bool(true)
bool(false)
bool(false)

How to Check if a Variable is an Array in php

There are many other functions which allow us to check the data type of different variables in php.

Another case is if you want to check if a variable is an array variable.

The php is_array() function checks if a variable is an array variable in the similar way to is_string() – just pass the variable you want to check to is_array().

Below is an example which shows you how to use is_array() to check if a variable is an array in php.

$array = array(1,2,3);
$float = 1.0;
$string = "This is a string";

var_dump(is_array($array));
var_dump(is_array($float));
var_dump(is_array($string));

// Output:
bool(true)
bool(false)
bool(false)

How to Check if a Variable is a Float in php

Another common case where you may want to check the data type of a variable is if you want to check if a variable is a float variable.

The php is_float() function checks if a variable is a float variable in the similar way to is_string() – just pass the variable you want to check to is_float().

Below is an example which shows you how to use is_float() to check if a variable is a float in php.

$array = array(1,2,3);
$float = 1.0;
$string = "This is a string";

var_dump(is_float($array));
var_dump(is_float($float));
var_dump(is_float($string));

// Output:
bool(false)
bool(true)
bool(false)

Hopefully this article has been useful for you to learn how to use the is_string() function in php to check if a variable is a string.

Other Articles You'll Also Like:

  • 1.  Check if String Ends with Given String in php with str_ends_with()
  • 2.  Capitalize First Letter of String with php ucfirst() Function
  • 3.  Get Current Location Latitude Longitude in php
  • 4.  How to Create an Empty Array in php
  • 5.  php array_slice() – Get Slice of Elements from Array
  • 6.  Get Query String from URL in php
  • 7.  Creating Custom category.php With Pagination
  • 8.  Using function_exists() to Check if Function Exists in php
  • 9.  php sleep() Function – Pause Execution of Code in php
  • 10.  php max – Find Maximum Value of Array in php

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy