PHP Strings

Strings are a type of variable, they are used to store a text, and the most of time to be used in the echo function.

creating a string variable is too easy !

PHP Code:
<?php
// creating a string with double-quotes
$string1 = "Wellcome !" ;

// creating a string with single quotes
$string2 = 'Wellcome !' ;

echo $string1 ;
echo $string1 ;
echo 'Wellcome !' ;

?>
Display
Wellcome ! Wellcome ! Wellcome !

In php you can put a text between either single quotes or double-quotes. And this is for both assignment of a text into a variable or when using echo.

In php there are no difference between:
<?php
$string1 = "Wellcome !" ;
echo $string1 ;
?>
Or:
<?php
echo "Wellcome !";
?>

Concatenation:
How can we add a string to a second string ? we know that for numbers, we can use 5+5=10, but how to do for string ?
Ok, the answer is very easy, you just use a dot:

In this example we will echo two string together:
<?php
$string1 = "Wellcome ! " ;
$string1 = "This is My PHP Source Blog !" ;
echo $string1 . $string2 ;
?>
Display:
Wellcome ! This is My PHP Source Blog !

And it's possible also to assign two strings to a third string:

<?php
$string1 = "Wellcome ! " ;
$string1 = "This is My PHP Source Blog !" ;
echo $string1 . $string2 ;
?>
Display:
Wellcome ! This is My PHP Source Blog !

Heredoc:
heredoc in php means a block of text that you will use as a string, this is more robust string creation tool than quotes, this allows the programmer to create a multiline string block:
<?php
$string1 = <<<myblock
Hi every body !
Wellcome to my web log !
This is the best php source for full free scripts
myblock;

echo $string1 ;
?>
Display:
Hi every body !Wellcome to my web log !This is the best php source for full free scripts
"<<<" means that there will start a new heredoc. "myblock" is the identifier of the heredoc, you can rename it anything you want. "myblock;" is the end of the heredoc, with the same identifier and followed by a semicolon, and it MUST occur on a LINE by itself !

PHP Strings and HTML codes !
You may ask yourself why the new line character in php didn't appear in the html display, let's take for example the first code in this page:

<?php
// creating a string with double-quotes
$string1 = "Wellcome !" ;

// creating a string with single quotes
$string2 = 'Wellcome !' ;

echo $string1 ;
echo $string1 ;
echo 'Wellcome !' ;

?>
Display
Wellcome ! Wellcome ! Wellcome !
You may want it to be:
Wellcome !
Wellcome !
Wellcome !


This is due to that the html script don't accept this new line character, in html to set a new line you must use <br />( or <br>)

In html <br /> mean a new line, so php should write <br /> tag to make this newline.
So now we have to learn how PHP script interact with HTML script:

the php code should be:
<html>
<head>
<title>My Page Title here</title>
</head>
<body>
<?php
$string1 = "Wellcome !" ;


$string2 = 'Hello !' ;

echo $string1 ;
echo '<br />' ;
echo $string1 ;

// and this is also possible:
echo $string1 . '<br />' . $string2 ;
?>
</body>
</html>
Display
Wellcome !
Hello !

This how php simply interact with html code ! And this is the power of php as a server side language!

0 comments: