creating a string variable is too easy !
PHP Code:
<?php
// creating a string with double-quotes
$string1 = "Wellcome !" ;
// creating a string with single quotes
echo $string1 ;
echo $string1 ;
?>
// creating a string with double-quotes
$string1 = "Wellcome !" ;
// creating a string with single quotes
$string2 = 'Wellcome !' ;
echo $string1 ;
echo $string1 ;
echo 'Wellcome !' ;
?>
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 ;
?>
$string1 = "Wellcome !" ;
echo $string1 ;
?>
<?php
echo "Wellcome !";
?>
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 ;
?>
$string1 = "Wellcome ! " ;
$string1 = "This is My PHP Source Blog !" ;
echo $string1 . $string2 ;
?>
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 ;
?>
$string1 = "Wellcome ! " ;
$string1 = "This is My PHP Source Blog !" ;
echo $string1 . $string2 ;
?>
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 ;
?>
$string1 = <<<myblock
Wellcome to my web log !
This is the best php source for full free scripts
myblock;
echo $string1 ;
?>
Hi every body !Wellcome to my web log !This is the best php source for full free scripts
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 !' ;
?>
// creating a string with double-quotes
$string1 = "Wellcome !" ;
// creating a string with single quotes
$string2 = 'Wellcome !' ;
echo $string1 ;
echo $string1 ;
echo 'Wellcome !' ;
?>
Wellcome ! Wellcome ! Wellcome !
Wellcome !
Wellcome !
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>
<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>
Wellcome !
Hello !
Hello !