Write String in PHP - String is a collection of characters. In PHP, characters are equal to bytes, where there are 256 characters. PHP does not support native-unicode.
To write a string in PHP, can use 3 (three) ways, namely by:
Program 1
File Name: string01.php
Description: Program string with single quoted (').
Open notepad, then type this php code.
<?php
echo 'this is a simple string';
echo 'You can also have embedded newlines in
strings this way as it is
okay to do';
// Outputs: Arnold once said: "I'll be back"
echo 'Arnold once said: "I\'ll be back"';
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\\*.*?';
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\*.*?';
// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline';
// Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either';
?>
Save it to the htdocs folder named string01.php. To see the result open the browser type http://localhost/string01.php in the address bar then enter.
Program 2
File Name: string02.php
Description: Program string with heredoc syntax.
Type the following php code into notepad.
<?php
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
/* More complex example, with variables. */
class foo
{
var $foo;
var $bar;
function foo()
{
$this->foo = 'Foo';
$this->bar = array('Bar1', 'Bar2', 'Bar3');
}
}
$foo = new foo();
$name = 'Johans';
echo <<<EOT
<u>$str</u><br>
My name is "<b>$name</b>". I am printing some <b>$foo->foo</b>.
Now, I am printing some <b>{$foo->bar[1]}</b>.
This should print a capital 'A': \x41
EOT;
?>
Save the php code with the name string02.php in the htdocs folder. To see the result, open the browser then type http://localhost/string02.php in the address bar then enter. The result will be like this.
That's how to write strings in php that we can explain. Hopefully the explanation can improve our ability in learning php programming language. Learn also about Functions in PHP.
To write a string in PHP, can use 3 (three) ways, namely by:
- single quoted - single quotes (')
- double quoted - double quotes (")
- heredoc syntax
Program 1
File Name: string01.php
Description: Program string with single quoted (').
Open notepad, then type this php code.
<?php
echo 'this is a simple string';
echo 'You can also have embedded newlines in
strings this way as it is
okay to do';
// Outputs: Arnold once said: "I'll be back"
echo 'Arnold once said: "I\'ll be back"';
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\\*.*?';
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\*.*?';
// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline';
// Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either';
?>
Save it to the htdocs folder named string01.php. To see the result open the browser type http://localhost/string01.php in the address bar then enter.
Program view |
File Name: string02.php
Description: Program string with heredoc syntax.
Type the following php code into notepad.
<?php
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
/* More complex example, with variables. */
class foo
{
var $foo;
var $bar;
function foo()
{
$this->foo = 'Foo';
$this->bar = array('Bar1', 'Bar2', 'Bar3');
}
}
$foo = new foo();
$name = 'Johans';
echo <<<EOT
<u>$str</u><br>
My name is "<b>$name</b>". I am printing some <b>$foo->foo</b>.
Now, I am printing some <b>{$foo->bar[1]}</b>.
This should print a capital 'A': \x41
EOT;
?>
Save the php code with the name string02.php in the htdocs folder. To see the result, open the browser then type http://localhost/string02.php in the address bar then enter. The result will be like this.
Program view |
That's how to write strings in php that we can explain. Hopefully the explanation can improve our ability in learning php programming language. Learn also about Functions in PHP.
Comments
Post a Comment