Eric The Coder
Posted on January 14, 2022
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";
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
It is also possible to join two strings with the operator "." (point)
$name = 'Mike';
echo 'Hello ' . $name;
// Hello Mike
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
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
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;
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
Remove white space before and after a string
$message = ' Hello World ';
echo trim($messsage);
// Hello World
Replace part of a string with another
$message = 'I love dog';
echo str_replace('dog', 'cat', $message);
// I love cat
Retrieve the number of characters in a string
$name = '';
echo strlen($name);
// 0
$name = 'Mike Taylor';
echo strlen($name);
// 11
Check if the string contains certain characters
$message = 'I love dogs and cats';
echo str_contains($message, 'dogs');
// true
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
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
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
Syntax increment
It is possible to do an incrementation in three ways:
// Increment, long method
$value = 10;
$value = $value + 1;
echo $value;
// 11
// Increment, sort method
$value = 10;
$value += 1;
echo $value;
// 11
// Increment, shorter method
$value = 10;
$value++;
echo $value;
// 11
It is also possible to decrement
$value -= 1;
// ou
$value--;
Round
It is possible to round to the nearest whole number
$value = 10.52;
echo round($value);
// 11
It is also possible to specify the number of digits to keep after the period.
$value = 10.5278;
echo round($value, 2);
// 10.53
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
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
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_
Posted on January 14, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.