DEV Community

Cover image for PHP crash course : Strings and Numbers
Eric The Coder
Eric The Coder

Posted on • Updated on

PHP crash course : Strings and Numbers

Today you will learn strings and numbers manipulation in PHP.

This PHP crash course is free and will be posted here on dev.to. I'll be releasing a new article/post every two days or so. To not miss anything, you can follow me on twitter: Follow @EricTheCoder_

String in PHP

Now let's see in a little more detail how to create and manipulate Strings with PHP.

A String can be created with single or double quotes

$name = 'Mike';

// or

$name = "Mike";
Enter fullscreen mode Exit fullscreen mode

The declaration with double quotes allows you to do interpolation.

// Display the content of a variable
echo "Hello $name";
// Hello Mike

// or some find this syntax easier to read
echo "Hello {$name}";
// Hello Mike
Enter fullscreen mode Exit fullscreen mode

It is also possible to join two strings with the operator "." (point)

$name = 'Mike';

echo 'Hello ' . $name;
// Hello Mike
Enter fullscreen mode Exit fullscreen mode

It is possible to read or modify a particular character using the brackets []

$message = "Hello World";

// Display
echo $message[0];
// H

// Or modify
$message[1] = 'a';
// Hallo World
Enter fullscreen mode Exit fullscreen mode

Here the first character is at position 0, the second at position 1, etc.

It is possible to access stating from the end.

$message = "Hello World";

echo $message[-1];
// d
Enter fullscreen mode Exit fullscreen mode

Position -1 represents the last character, -2 the second last, and so on.

Heredoc

This syntax allows you to enter a string on several lines

$html = <<<HTML
<h1>This is a title</h1>
<p>This is a paragraphe</p>
HTML;
Enter fullscreen mode Exit fullscreen mode

The $html variable will include all the text between the two HTML keywords. Including spaces and newlines.

Handling the Strings

PHP has several functions that allow you to transform the content of the String. Here are a few :

Uppercase and lowercase conversion

$name = 'Mike Taylor';

echo strtolower($name); 
// mike taylor

echo strtoupper($name);
// MIKE TAYLOR
Enter fullscreen mode Exit fullscreen mode

Remove white space before and after a string

$message = '  Hello World        ';

echo trim($messsage);
// Hello World
Enter fullscreen mode Exit fullscreen mode

Replace part of a string with another

$message = 'I love dog';

echo str_replace('dog', 'cat', $message);
// I love cat
Enter fullscreen mode Exit fullscreen mode

Retrieve the number of characters in a string

$name = '';
echo strlen($name);
// 0

$name = 'Mike Taylor';
echo strlen($name);
// 11
Enter fullscreen mode Exit fullscreen mode

Check if the string contains certain characters

$message = 'I love dogs and cats';

echo str_contains($message, 'dogs');
// true
Enter fullscreen mode Exit fullscreen mode

If the string contains ‘dog’ the function will return true otherwise it will return false

The use of this kind of function will be more obvious when we study the conditional statement (if)

PHP has several functions for handling strings. Before creating your own function, do a little research in the documentation because there is a good chance that this function already exists.

Number in PHP

Numbers are used to perform mathematical operations such as addition, subtraction, multiplication, and division.

$value = 10;

echo $value + 5;
// 15

echo $value - 2;
// 8

echo $value * 2;
// 20

echo $value / 2;
// 5
Enter fullscreen mode Exit fullscreen mode

Operator priority https://www.php.net/manual/en/language.operators.precedence.php

When we do multiple operations PHP will take into account the priority of the operators. That is, the order in which the values should be analyzed. Here is an example:

echo 1 + 5 * 3
// 16
Enter fullscreen mode Exit fullscreen mode

Here the answer will be 16 and not 18. because the multiplication "*" has a higher priority compared to the addition "+".

Parentheses can be used to force the priority, if necessary.

echo (1 + 5) * 3
// 18
Enter fullscreen mode Exit fullscreen mode

Syntax increment

It is possible to do an incrementation in three ways:

// Increment, long method

$value = 10;

$value = $value + 1;

echo $value;
// 11
Enter fullscreen mode Exit fullscreen mode
// Increment, sort method

$value = 10;

$value += 1;

echo $value;
// 11
Enter fullscreen mode Exit fullscreen mode
// Increment, shorter method

$value = 10;

$value++;

echo $value;
// 11
Enter fullscreen mode Exit fullscreen mode

It is also possible to decrement

$value -= 1;

// ou
$value--;
Enter fullscreen mode Exit fullscreen mode

Round

It is possible to round to the nearest whole number

$value = 10.52;

echo round($value);
// 11
Enter fullscreen mode Exit fullscreen mode

It is also possible to specify the number of digits to keep after the period.

$value = 10.5278;

echo round($value, 2);
// 10.53

Enter fullscreen mode Exit fullscreen mode

Random number

The rand function allows you to create a random integer whose value will be included between the values of the two parameters.

echo rand(10, 100); 
// 89
Enter fullscreen mode Exit fullscreen mode

Readability of numbers

You can use the character _ as a separator. This helps to make the number easier to read.

$value = 1_000_000_000
// 1000000000
Enter fullscreen mode Exit fullscreen mode

Conclusion

That's it for today, I'll be releasing a new article every two days or so. To be sure not to miss anything you can follow me on twitter: Follow @EricTheCoder_

Discussion (3)

Collapse
suckup_de profile image
Lars Moelleken

Hi, thanks for your post. 👍

note: php still haven't support for unicode chars by default, so you need to use "mb_str*" functions instead e.g. mb_strtoupper or you directly use a OOP wrapper for strings like this: github.com/voku/Stringy

Bests
Lars

Collapse
qferrer profile image
Quentin Ferrer • Edited on

Thanks for this great post! In this following example you shared:

$message = "Hello World";

echo $message[-1];
// e
Enter fullscreen mode Exit fullscreen mode

The result is d and not e 😄

Greetings,

Quentin

Collapse
ericchapman profile image
Eric The Coder Author

Oupss thanks. Corrected.