Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

is_string
php
function

What is the is_string method in PHP?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The is_string method can be used to check if the type of the variable is string or not.

Syntax

is_string(mixed $value): bool

This method returns true if the passed value is an string. Otherwise, it returns false.

Code

<?php 

  $test = 'str';
  printIsString($test);

  $test = '';
  printIsString($test);

  $test = true;
  printIsString($test);

  $test = 10;
  printIsString($test);

  function printIsString($test){
    echo "The Value is: ";
    var_dump($test);

    echo "Is String: ";
    var_dump(is_string($test));
    
    echo "---------\n\n";
  }

?>

Explanation

In the above code:

  • We used the is_string method to check if a variable is a string or not.

  • For the value str and "", the is_string method returns true.

  • But for the value true(boolean) and 10(int), the is_string method returns false because it is not a string type variable.

RELATED TAGS

is_string
php
function

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring