By Yoav David | 8/7/2016 | PHP

PHP Tutorial - Part 2

Here Document:

Sometimes we want to print a text in a specific way that isn’t available through the previous printing methods. In these cases, we can use here documents, which will allow us to align the text any way we want. Here documents are written like this:

<?php

echo <<<YourDocumentName
YourText
YorDocumentName;

?>

Keep in mind that every here document starts with “echo <<<”. Another thing you should pay attention to is that there is absolutely nothing else than the document’s name and a “;” right after it, or you will get an error message.

 

Print And Echo:

Both commands are written the same syntax and basically give the results, but there is one difference between them - ‘echo’ can print more than one parameter (several parameters divided by commas), while ‘print’ cannot.

 

Changing To Upper Or Lower Cases:

PHP has two built-in functions which are dedicated for changing all the letters in a string into upper or lower cases.

To switch all the letters in a string into uppercase, use the ‘strtoupper’ function:

<?php

$VariableName = “We Will Rock You!”;
Echo strtoupper($VariableName);

?>

And the output will be:

WE WILL ROCK YOU!

To switch all the letters in a string into uppercase, use the ‘strtolower’ function:

<?php

$VariableName = “We Will Rock You!”;
Echo strtolower($VariableName);

?>

And the output will be:

we will rock you!

 

Checking a string’s length:

In PHP we can also check how many characters the are in a certain string. To do so, we use the ‘strlen()’ function:

<?php

$VariableName = “We Will Rock You!”;
echo strlen($VariableName);

?>

And the output will be:

17

Another way to use the ‘strlen()’ function is as a parameter:

<?php

$VariableName = “We Will Rock You!”;

echo “The string: “, “\””, $VariableName, “\””;
echo “\n”;
echo “The string’s length: ”, strlen($VariableName), “ characters”;

?>

And the output will be:

The string: “We Will Rock You!”
The string’s length: 17 characters


String Position:

We can find the position of a letter/word/phrase inside a certain string in PHP. this is done by using the ‘strpos()’ command, in the following syntax:
strpos(StringOrVariable, “TheDesiredLetters”, OptionalNumber). It is done this way:

<?php

$VariableName = “Hey, Anybody? Find me somebody to love”
echo strpos($VariableName, “e”);

?>

Or:

<?php

echo strpos(“Hey, Anybody? Find me somebody to love”, “e”);

?>

In both cases, the output will be:

1

Keep in mind that the search is case-sensitive, and also that the counting starts from 0.

The third and optional parameter that can be used is meant for cases in which we want to find another positions in which the letter\word\phrase appears other than the first position. The third parameter represents the position in the string which we want the function to start searching from. For example:

<?php

$VariableName = “Hey, Anybody? Find me somebody to love”
echo strpos($VariableName, “e”, 2);

?>

And the output will be:

20

If the function can’t find what it was asked to find, the output will be an empty string (the function won’t return anything to display).

 

String Replacement:

Sometimes we want to replace a letter or a word in a string, and for this purpose PHP offers us the ‘str_replace()’ function, which is used in this syntax:
str_replace(“TargetWord”, “DesiredWordInstead”, TargetString, OptionalCountingVariable). For example:

<?php

$VariableName = “Is that so?”;
echo str_replace(“so”, “you”, $VariableName);

?>

We can also use this function as a given value to a variable:

<?php

$VariableName = “Is that so?”;
$VariableName2 =  str_replace(“so”, “you”, $VariableName);
echo $VariableName2;

?>

In both ways the output will be:

Is that you?

We can also use an optional fourth parameter which is meant for counting the times a replacement was made by the function:

<?php

$VariableName = “Is that so?”;
echo str_replace(“so”, “you”, $VariableName, $OptionalCountingVariable);
echo “\n”, $OptionalCountingVariable;

?>

And the output will be:

Is that you?
1

Keep in mind that the counting variable doesn’t need to be declared before being used in the function - it is being declared and used altogether in the function itself.

Keep in mind that using the function is case-sensitive, and if the function won’t find any matches it will print the original string, without and changes.

 

Removing A Certain Part From A String:

In order to remove a certain part from a string, we’ll use the ‘substr()’ command. The syntax is:
substr($TargetString, StartingPositionNumber, OptionalEndingPositionNumber). The command works in a way that everything before the position given inside the function will be removed. For example:

<?php

$VariableName = “What is the meaning of life?”;
echo substr($VariableName, 5);

?>

And the output will be:

is the meaning of life?

We can also use a negative number to leave a certain amount of characters in the string, when the counting starts from the right:

<?php

$VariableName = “What is the meaning of life?”;
echo substr($VariableName, -3);

?>

And the output will be:

fe?

There is also an optional variable which presents the length of the desired new string (every character after the length’s end will be removed as well):

<?php

$VariableName = “What is the meaning of life?”;
echo substr($VariableName, 8, 16);

?>

And the output will be:

the meaning of l

We can use negative number here as well:

<?php

$VariableName = “What is the meaning of life?”;
echo substr($VariableName, 8, -3);

?>

And the output will be:

the meaning of li

 

Splitting A String:

In PHP we can also split a string by converting it into an array. It is done with the ‘str_split()’, in the following syntax: str_split(TargetString, OptionalLengthVariable). For example:

<?php

$VariableName = “What is the meaning of life?”;
$VariableName2 = str_split($VariableName);
print_r($VariableName2);

?>

And the output will be:

Array

(

    [0] => W

    [1] => h

    [2] => a

    [3] => t

    [4] =>

    [5] => i

    [6] => s

    [7] =>

    [8] => t

    [9] => h

    [10] => e

    [11] =>

    [12] => m

    [13] => e

    [14] => a

    [15] => n

    [16] => i

    [17] => n

    [18] => g

    [19] =>

    [20] => o

    [21] => f

    [22] =>

    [23] => l

    [24] => i

    [25] => f

    [26] => e

    [27] => ?

)

There is also another optional parameter which can be used in cases when we want to split the string by chunks of strings instead of by individual letters:

<?php

$VariableName = “What is the meaning of life?”;
$VariableName2 = str_split($VariableName, 6);
print_r($VariableName2);

?>

And the output will be:

Array

(

    [0] => What i

    [1] => s the

    [2] => meanin

    [3] => g of l

    [4] => ife?

)

{{CommentsModel.TotalCount}} Comments

Your Comment

{{CommentsModel.Message}}

Recent Stories

Top DiscoverSDK Experts

User photo
3355
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
3220
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
User photo
3060
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
Show All
X

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.

{{compareToolModel.Error}}

Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now