Lesson 4: PHP Strings

Written by admin

Topics: Lessons

A string is just a sequence of characters. In PHP, strings are enclosed in quotes such as ” or ‘.

Strings can be printed or assigned to variables. Let’s take a look at an example.

<?php
  $my_string_variable = "This is a string" . ' and so is this';
  print $my_string_variable;
  print "\nHere's another string";
?>

The output of this code is:

This is a string and so is this
Here’s another string

The code above makes use of two new characters: ‘\n’ and ‘.’. ‘.’ concatenates two strings. Concatenation just means it “glues” together two strings. The ‘\n’ character means to continue the output on the next line. Literally it means, “new” line.

Moving on. strlen() is a tool you need. It is probably the most important “string” function. It simply tells you how many characters are in a string. print strlen(“hi”); would print 2 to the screen.

For a bit more about strings check out the PHP instr() tutorial.

3 Comments Comments For This Post I'd Love to Hear Yours!

  1. Greg Keane says:

    is that it 4 lessons? will there be more?

  2. Akila says:

    hey.. i really like your tutorials.. Is this the end ? please add more and more about PHP… Kudos..

Leave a Comment Here's Your Chance to Be Heard!