You are on page 1of 11

CHAPTER 1

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

PHP (Hypertext Preprocessor) is a widely-used server-side scripting language that


is especially suited for Web development and can be embedded into HTML. Much of the syntax
of PHP is borrowed from C, Java, and Pearl with some unique PHP features.

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.

Advantages of choosing PHP:


1. PHP supports many databases (MYSQL, ORACLE).
2. PHP is an open source software that can be downloaded from the official PHP resource
(www.php.net)
3. PHP runs on different browsers (Chrome, Firefox, Safari) and platforms (Windows, Mac
OS, Linux).
4. PHP is compatible with almost all web servers (Apache, IIS)
5. PHP is easy to learn.

What is a PHP File?


1. PHP file can contains text, HTML Tags and Scripts.
2. PHP file are returned to web browsers as plain HTML.
3. PHP file have an extension name of “.php”, “.php3”, “.phtml”

Requirements in running PHP


1. Webserver (Apache or IIS)
2. Text Editor (Dreamweaver or Notepad or Notepad ++)

2 PREPARED BY: MS. APRIL VALENCIANO ASILO |


Chapter 1: Introduction to PHP

Inserting PHP codes

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>

Running of PHP Programs:


1. Create your folder on the HTDOCS Folder located on the installation path of
your
XAMPP.

2. Save your work in the newly created folder.


Note: Save your work with the extension name of “.php”.
3. Open the installation path of your XAMPP and double click the file “xampp-control”.

3 PREPARED BY: MS. APRIL VALENCIANO ASILO |


Chapter 1: Introduction to PHP

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.

5. Open any web browser and in the URL box type


“localhost/<folder_name>/<file_name>.php”
Example:

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):

Example (w/o ERROR):

4 PREPARED BY: MS. APRIL VALENCIANO ASILO |


Chapter 1: Introduction to PHP

Inserting Comments in PHP

<?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.

5 PREPARED BY: MS. APRIL VALENCIANO ASILO |


Chapter 1: Introduction to PHP

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.

Variable Naming Rules in PHP

PHP variables must start with a letter or underscore ( _ ).


PHP variables may only be comprised of alpha-numeric characters and underscore ( _ ).
Variable names with more than one word should be separated by underscore ( _ )
or differentiate by capitalization.
o $my_variable, $myVariable
Variable names are case-sensitive
o $myvariable is different from $myVariable

Displaying Values in PHP


In displaying values in PHP echo or print is being used.
Example:
<?php
$text = "sample";
$number = 1;
echo $text;
print $number;
?>
echo will display the value of $text variable.
print will display the value of $number variable.

6 PREPARED BY: MS. APRIL VALENCIANO ASILO |


Chapter 1: Introduction to PHP

Single Quotation VS. Double Quotation

Single quote displays the value inside of it.


Double quote displays the content of inside of it.
Example:
<?php
$text = 'sample';
echo '$text'; // displays $text
echo '<br>';
echo "$text"; //displays sample
?>

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”.

Manipulating Strings in PHP

String variables are used to store multiple set of characters.


Here are some common functions and operators used to manipulate strings:
o Concatenation Operator
is used to put two (2) strings together.
Dot (.) is the concatenation operator in PHP.
Example:
<?php
$text1="sample";
$text2="1111";
echo $text1 . " " . $text2;
//output will be sample1111
?>

7 PREPARED BY: MS. APRIL VALENCIANO ASILO |


Chapter 1: Introduction to PHP

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).

Be careful when echoing your strings with quotation marks:


o Escape your quotes that are within the string with a backslash.
o Escaping means placing a backslash directly before the quotation mark, i.e.
\" or \'.

8 PREPARED BY: MS. APRIL VALENCIANO ASILO |


Chapter 1: Introduction to PHP
Example:
<?php
// This won't work because of the quotes around specialH5!
echo "<h5 class="specialH5">I love using PHP!</h5>";

// OK because we escaped the quotes!


echo "<h5 class=\"specialH5\">I love using PHP!</h5>";

// OK because we used an apostrophe '


echo "<h5 class='specialH5'>I love using PHP!</h5>";
?>

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!
?>

9 PREPARED BY: MS. APRIL VALENCIANO ASILO |


Chapter 1: Introduction to PHP

Things to be remembered in using HEREDOC

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.

10 PREPARED BY: MS. APRIL VALENCIANO ASILO |


Chapter 1: Introduction to PHP

11 PREPARED BY: MS. APRIL VALENCIANO ASILO |

You might also like