Can a single variable store both digits and characters in PHP?
Yes. If you put digits and characters together inside quotes, PHP stores them as a string. PHP is a loosely typed language, so you do not need to declare the type.
<?php
$variable = "Contact:24/7";
echo $variable . "<br>";
echo gettype($variable) . "<br>";
// The same variable can store another type later
$variable = 24;
echo gettype($variable);
?>
Output:
Tutorials Class - Output Window
Contact:24/7
string
integer
Remember that the value in quotes, like "24", is a string and not a number, even if it has only digits. Read PHP Data Types for more.
Learn more about the similar topics:
| Tutorials |
|---|
| PHP Variables |
| PHP Data Types |
| PHP Constants |
| PHP isset() Function |
| PHP empty() Function |
| PHP is_null() Function |
| Exercises & Assignments |
|---|
| Write a program to print “Hello PHP” using variable |
| Write a program to print a string using echo+variable. |
| Write a program to print two variables in single echo |