In PHP, strings and numbers are two of the most commonly used data types. Letβs explore how to work with both effectively.
A string is a series of characters, like "Hello World!" or "Technorank".
Examples of strings:
<?php $text1 = "Hello World!"; $text2 = 'Technorank Solutions'; echo $text1; ?>
strlen() β Returns the length of a stringstrtoupper() β Converts to uppercasestrtolower() β Converts to lowercasestr_replace() β Replaces text within a stringstrpos() β Finds the position of text in a string$text = "Technorank is cool!"; echo strlen($text); // Output: 20 echo strtoupper($text); // Output: TECHNORANK IS COOL!
PHP supports integers (whole numbers) and floats (decimal numbers).
Examples of numbers:
<?php $x = 10; // Integer $y = 5.5; // Float echo $x + $y; // Output: 15.5 ?>
is_int() β Checks if value is an integeris_float() β Checks if value is a floatround() β Rounds a numbernumber_format() β Formats a number with grouped thousands$num = 1234.567; echo round($num); // Output: 1235 echo number_format($num, 2); // Output: 1,234.57
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!