PHP crash course : Strings and Numbers
Tags: php, backends, tutorial, beginners
Published: 2022-01-14 14:52:55

---
title: PHP crash course : Strings and Numbers
published: true
description: 
tags: php, backends, tutorial, beginners
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rpfsad93u38r3xbmk8s7.png
series: ModernPHP
---
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: <a href="https://twitter.com/EricTheCoder_" class="twitter-follow-button" data-show-count="false">Follow @EricTheCoder_</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>

## 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
```php
$name = 'Mike';

// or

$name = "Mike";
```

The declaration with double quotes allows you to do interpolation.

```php
// 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)
```php
$name = 'Mike';

echo 'Hello ' . $name;
// Hello Mike
```

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

```php
$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.
```php
$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
```php
$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
```php
$name = 'Mike Taylor';

echo strtolower($name); 
// mike taylor

echo strtoupper($name);
// MIKE TAYLOR
```

Remove white space before and after a string
```php
$message = '  Hello World        ';

echo trim($messsage);
// Hello World
```

Replace part of a string with another
```php
$message = 'I love dog';

echo str_replace('dog', 'cat', $message);
// I love cat
```

Retrieve the number of characters in a string
```php
$name = '';
echo strlen($name);
// 0

$name = 'Mike Taylor';
echo strlen($name);
// 11
```

Check if the string contains certain characters
```php
$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.

```php
$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:
```php
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.
```php
echo (1 + 5) * 3
// 18
```

### Syntax increment

It is possible to do an incrementation in three ways:
```php
// Increment, long method

$value = 10;

$value = $value + 1;

echo $value;
// 11
```

```php
// Increment, sort method

$value = 10;

$value += 1;

echo $value;
// 11
```

```php
// Increment, shorter method

$value = 10;

$value++;

echo $value;
// 11
```

It is also possible to decrement
```php
$value -= 1;

// ou
$value--;
```

### Round

It is possible to round to the nearest whole number
```php
$value = 10.52;

echo round($value);
// 11
```

It is also possible to specify the number of digits to keep after the period.
```php
$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.

```php
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.
```php
$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: <a href="https://twitter.com/EricTheCoder_" class="twitter-follow-button" data-show-count="false">Follow @EricTheCoder_</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>