Home >Backend Development >PHP Problem >How to determine if a variable is a string in php

How to determine if a variable is a string in php

青灯夜游
青灯夜游Original
2021-09-15 19:16:583713browse

In PHP, you can use the is_string() function to determine whether a variable is a string, the syntax is "is_string ($var)"; if the specified variable is a string, TRUE is returned, otherwise FALSE is returned.

How to determine if a variable is a string in php

The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer

is_string() function is used to detect whether the variable is String. Syntax format:

bool is_string ( mixed $var )

Parameter description:

  • $var: variable to be detected.

PHP version requirements: PHP 4, PHP 5, PHP 7

Return value: If the specified variable is a string, then Returns TRUE, otherwise returns FALSE.

Example: is_string() function determines whether a variable is a string

<?php
header("Content-type:text/html;charset=utf-8");
$a=123;
var_dump($a);
var_dump(is_string($a));
if (is_string($a)){
    echo "这是一个字符串。". "<br>";
}
else{
    echo "这不是一个字符串。". "<br>";
}
$b="123";
var_dump($b);
var_dump(is_string($b));
if (is_string($b)){
    echo "这是一个字符串。". "<br>";
}
else{
    echo "这不是一个字符串。". "<br>";
}
?>

Output result:

How to determine if a variable is a string in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to determine if a variable is a string in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn