Cameron Garnett/Testpage8

From WikiEducator
Jump to: navigation, search
PHP

Super Quick PHP Tutorial

Introduction | Basic Syntax | Sending Data to the Browser | Comments | Variables | Strings | Functions | Quotes | Debugging | Form


VmvIcon Objectives.png

Learning Objectives

By the end of this page you will understand what a string is and a few basic commands that you can use to manipulate strings.

Strings

A string is a sentence or phrase like "I am the greatest!".

In this next example I will show you the difference between the concatenation operator and adding two strings together.

VmvIcon Example.png

Example

<?php

echo "seven"."teen"; //prints the string "seventeen"
echo "twe" . "nty"; //prints the string "twenty"
echo 5 . 6; //prints the string "56"
echo 5.6; //prints the number 5.6
echo 5+6; //prints the number 11

?>


The next example is how to return the length of a string using the strlen() function.

VmvIcon Example.png

Example

<?php
echo strlen("Hello world!"); // outputs 12
?>


The next example is how to return how many words in a string using the str_word_count function.

VmvIcon Example.png

Example

<?php
echo str_word_count("Happy New Year!"); // outputs 3
?>


The next example works similar to javascript splitting a string into an array using the str_split() function.

VmvIcon Example.png

Example

<?php
print_r(str_split("Count")); //the out put would be Array ( [0] => C [1] => o [2] => u [3] => n [4] => t )
?>


VmvIcon Objective.png

ACTIVITY

Try writing the example blocks of code into notepad ++ and bare witness to the amazing power of PHP.