Professional Documents
Culture Documents
Chapter 1 - Introduction To PHP
Chapter 1 - Introduction To PHP
INTRODUCTION TO PHP
OBJECTIVES:
At the end of the chapter the student are expected to:
Identify what is PHP, its uses, and its advantages.
Insert a PHP code on a HTML File.
Insert a comment on the PHP code.
Learn on how to declare and use a variable in PHP.
Learn on how to display a value or a variable value in PHP.
Learn on how to manipulate strings in PHP.
Chapter 1: Introduction to PHP
When someone visits your PHP webpage, the webserver processes the PHP code.
The webserver then checks which parts it needs to show to the viewer of the webpage (web
contents) and hides the PHP code (operations) then converts the PHP code into HTML.
After the conversion, it sends the webpage to the viewer web browser.
1. PHP scripting blocks always starts with <?php and ends with ?>.
2. PHP scripting blocks can be placed anywhere in the file.
3. All statements must end with semicolon (;).
Semicolon acts as the separator and is used to differentiate one statement from
another.
Example:
<?php
echo "Hello World!";
?>
<html>
<body>
</body>
</html>
4. A dialog box will appear, Click the start button on the Apache Web Service. After
Clicking, the status of the Apache will be Running.
6. The browser will show the line number of your error, if your program does
not contain any error it will display the output of your program. If your program
contains error/s, open again your program, debug the error, save your program, and
run again. Example (w/ ERROR):
<?php
// This is a single-line comment
/*
This is
a comment
block
*/
?>
<html>
<body>
</body>
</html>
Variables in PHP
Variables are used for storing values like text, strings, numbers or even arrays.
When a variable is declared, it can be used frequently in the script.
All variables in PHP starts with “$” symbol.
The correct way in declaring PHP variable:
o $var_name=value;
Variables in PHP do not need to be declared before setting.
PHP automatically converts the variable to its correct data type depending on
how they are set.
PHP variable is automatically declared while being used.
Example:
<?php
$text=”Hello”; //String data type
$num=1; //Numeric data type
?>
$text is a String Variable because of its value is a String Constant.
$num is a Numeric Variable because of its value is a Numeric Constant.
The statement echo „$text‟ will display the word $text because of the $text is enclosed in
single quotation („‟) while the statement echo “$text” will display the content of the
$text variable which is the string “sample”.
o strpos() Function
is used to search a character within a string.
If the function found a match, it will return the position (index) of the first
match otherwise, it will return FALSE.
The first position (index) starts at 0.
Example:
<?php
echo strpos("Hello world!","world");
// output: 6
?>
o strtolower() Function
converts a string to lowercase letters.
o strtoupper() Function
converts a string to uppercase letters.
Example:
<?php
echo strtolower(“Sample”);
echo strtoupper(“Sample”);
// output: sample SAMPLE
?>
The first statement in the example will convert the string “Sample” into lowercase
letters while the second statement will convert the string “Sample” into uppercase letters.
For the complete list of String functions, visit the PHP resource (www.php.net).
HEREDOC Statement
Heredoc statement lets programmer to create multi-line strings without the using
quotations.
Note: Creating a string using heredoc statement is more complicated and can lead
to
problems if the programmer doesn‟t properly code the string.
Example:
<?php
$my_string = <<<TEST
Computer Science students
are the best!
TEST;
echo $my_string;
// output: Computer Science students are the best!
?>
Use <<< and some identifier that you choose to begin the heredoc. In this
example we chose TEST as our identifier.
Repeat the identifier followed by a semicolon to end the heredoc string creation.
In this example that was TEST;
The closing sequence TEST; must occur on a line by itself and cannot be
indented!
Another thing to note is that when you output this multi-line string to a web page,
it will not span multiple lines because we did not have any <br /> tags contained
inside our string.