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
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.