Strings

PHP defines variables automatically. In other words, you do not need to tell PHP whether a variable is a string, a number or true/false etc. To set a value for a string, use =. Use $ to denote a variable name. For example:

$error = "Things have gone wrong";

$dog = "Rover";


You can create new strings by adding two existing strings together. Use a dot (.) to concatenate strings. For example:

$combined = $one.$two;
$fullname = $firstname.$lastname;

You can add a string to the end of existing string using the shorthand .= symbol. Therefore,

$text = $text.$newtext;
is the same as
$text .= $newtext;


Here are some working examples:

<?php
$hello = "Hello";
$world = " world";
$total = $hello.$world;
echo $total;
?>

This would cause the text "Hello world" to printed to the screen.


<?php
$hello = "Hello";
$hello .= " world";
echo $hello;
?>

This would also cause the text "Hello world" to printed to the screen, but is a bit more effecient that the first example.



Next article: Loops and decisions

Other variables

Numbers are assigned in the same way as strings. For example,

$first = 1;
$day = 24;


Use the normal maths symbols to manipulate numbers. For example:

$two = 1 + 1;
$total = $part1 + $part2;
$result = $one * $two;
$divide = $big / $small;
$net = $gross - $vat;


Variables can also be boolean (true of false). For example,

$correct = true;
$wrong = false;


Next article: Loops and decisions

Substring

There are lots of functions built into PHP that allow you to manipulate variables. Let's look at how to extract strings out from another string using substring.

Use substr to extract part of a string. The format is:
substr(stringname, startpos, length)
where stringname is the name of the full string from which you want to extract something
startpos is an integer that defines the start position for the substring (NOTE: the first character is position 0) length is the length of the substring you want. If you don't give this, PHP assumes you want the rest of the string

Examples:

$stringname = "Substring example";
$first = substr($stringname, 2);

// Returns "bstring example" into $first because startpos of 2 is the b.

$second = substr($stringname, 6, 2);
// Returns "in" into $second



Next article: Loops and decisions

Other functions

The easiest way to get to know how to use a particular function is to look at that function in the function list on the PHP website. Here is a list of some common ones with links to examples and the function page on the PHP site:

cos() Examples Function Manual
Calculates cosine of a number


html_entities() Examples Function Manual
Converts any special characters in a string to their HTML equivalents


ltrim() Examples Function Manual
Trims any blanks from the left of a string


mt_rand() Examples Function Manual
Generates a random number


nl2br() Examples Function Manual
Converts a new line character into a break HTML tag


pow() Examples Function Manual
Calculates one number raised to the power of another


round() Examples Function Manual
Rounds a number to a number of decimal places


rtrim() Examples Function Manual
Trims any blanks from the right of a string


sin() Examples Function Manual
Calculates the sine of a number


sqrt() Examples Function Manual
Calculates the square root of a number


str_pad() Examples Function Manual
Extends the length of a string by adding blanks to it


str_replace() Examples Function Manual
Replaces part of one string with other specified characters


str_word_count() Examples Function Manual
Counts the number of words in a string


strlen() Examples Function Manual
Returns the length of a string


strpos() Examples Function Manual
Finds the position of one string within another


strtolower() Examples Function Manual
Converts a string to all lowercase


strtoupper() Examples Function Manual
Converts a string to all uppercase


tan() Examples Function Manual
Calculates the tangent of a number


Next article: Loops and decisions

Previous

The previous article tells you how to show things on the screen using PHP and HTML combined.

Next

The next screen gives you a tutorial on how to use loops and handle decisions in PHP.
This tutorial tells you how to repeat things using loops and how to do things depending on whether something meets a given condition.

External Links

Summary

  • Use $ before words to show they are variables
  • PHP decides automatically whether a variable is a string, a number or true/false
  • Use equals(=) to assign values to variables.
  • Use dot(.) to add strings together
  • Use functions like substr to manipulate strings
  • Use maths symbols (+,-,*,/) to use with numbers
  • Use functions like sin to manipulate numbers