Integer Float and String Data Types
×
Home Courses Community Support
Integer Float and String Data Types

Now you know about both Data Types; constant and variables. Let's proceed with String data type.

String variables are just a sequence of characters and can be of any length. In this heading we are going to look at the most common functions and operators used to manipulate strings in PHP. A string can be used directly in a function or it can be stored in a variable.

enlightenedIn previous two headings of this module, the value passed in variable and constant, are also the string.

Run below code:

<?php
$txt = "I am Geeku.";
echo $txt;
?>

Output:

I am Geeku.

Let’s play with string

Count the String Length

The strlen() function is used to return the length of a string. Let's find the length of a string:

<?php
echo strlen("Count Length");
?>

Output:

12

The length of a string is often used in loops or other functions, when it is important to know when the string ends. (i.e. in a loop, we would want to stop the loop after the last character in the string).

Check Character Position in String

The strpos() function is used to search for character within a string. If a match is found, this function will return the position of the first match. If no match is found, it will return FALSE. Let's see if we can find the string "world" in our string:

<?php
echo strpos("I am Geeku", "Geeku");
?>

Output:

5

The position of the string "Geeku" in our string is position 5. The reason for that it is 5 (and not 6), is that the first position in the string is 0, and not 1.

Count words in a String

To count the number of words in a string we use 'str_word_count()' function. It will show you the exact number of words used in a string. See below example.

<?php
echo str_word_count("I am Geeku");
?>

Output:

3

Revers the String Characters

To reverse the characters used in string we use the function strrev(). This function will reverse the character's.

Check the below code:

<?php
echo strrev("I am Geeku");
?>

Output:

ukeeG ma I

Likely above mentioned string functions we have much more to work with string.

Integer and Float Data Type

Integer and Float both have numeric values. Integer is sole or combination of any value from 0-9. And a float is combination of same values including a decimal point (.) in between. Check below code, printing both; Integer value and Float value:

<?php
$int = "20";
$flt = "20.5";
echo "Integer value is: $int";
echo "<br>";
echo "Float value us: $flt";
?>

Output:

Integer value is: 20
Float value us: 20.5

enlightened<BR> is a HTML tag to break line. It cab be used in PHP.

 

← Preview

Using Constant Values

Next →

Other Data Types

NOTES

Our mission is to provide highly effective and quality education via innovative solutions. Geeksdemy look forward to bridge the gap between in-demand technology and academics in order to deliver innovative, easy, interesting and affordable learning across the Globe.

Courses

  • Python
  • Game Development with Unity 3D
  • Arduino
  • PCB & Circuit Designing
  • Robotics and Embedded C
  • 8051/8052 Embedded Systems
  • QuadCopter & UAV
  • MATLAB with Robotics
  • Raspberry Pi
Learn On the Go!
Get the Android App
Get the iOS App