0% found this document useful (0 votes)
47 views

Introduction To PHP: What You Can Do With PHP

PHP is a widely used server-side scripting language used to create dynamic web pages. PHP code is executed on the server and the output is sent to the browser as HTML. You can use PHP for tasks like dynamically generating web pages, accessing databases, sending emails, and more. PHP files have a .php extension and contain text, HTML, CSS, JavaScript, and PHP code. Variables in PHP start with a $ sign and are case-sensitive. The echo statement outputs strings, variables, and other data to the browser.

Uploaded by

Sibtul Hassan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Introduction To PHP: What You Can Do With PHP

PHP is a widely used server-side scripting language used to create dynamic web pages. PHP code is executed on the server and the output is sent to the browser as HTML. You can use PHP for tasks like dynamically generating web pages, accessing databases, sending emails, and more. PHP files have a .php extension and contain text, HTML, CSS, JavaScript, and PHP code. Variables in PHP start with a $ sign and are case-sensitive. The echo statement outputs strings, variables, and other data to the browser.

Uploaded by

Sibtul Hassan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lecture 17 Web Systems and Technologies

Introduction to PHP
PHP is the most popular server-side scripting language for creating dynamic web pages.
PHP stands for Hypertext Preprocessor.
PHP is a very popular and widely-used open source server-side scripting language to
write dynamically generated web pages.
PHP was originally created by Rasmus Lerdorf in 1994.
It was initially known as Personal Home Page.

What You Can Do with PHP


There are lot more things you can do with PHP.

You can generate pages and files dynamically.


You can create, open, read, write and close files on the server.
You can collect data from a web form such as user information, email, phone no, etc.
You can send emails to the users of your website.
You can send and receive cookies to track the visitor of your website.
You can store, delete, and modify information in your database.
You can restrict unauthorized access to your website.
You can encrypt data for safe transmission over internet.

What is a PHP File?


PHP files can contain text, HTML, CSS, JavaScript, and PHP code
PHP code is executed on the server, and the result is returned to the browser as plain
HTML
PHP files have extension ".php"

Advantages of PHP over Other Languages


Easy to learn: PHP is easy to learn and use. For beginner programmers who just
started out in web development, PHP is often considered as the preferable choice of
language to learn.
Open source: PHP is an open-source project. It is developed and maintained by a
worldwide community of developers who make its source code freely available to
download and use.
Lecture 17 Web Systems and Technologies

Portability: PHP runs on various platforms such as Microsoft Windows, Linux, Mac
OS, etc. and it is compatible with almost all servers used today such Apache, IIS, etc.
Fast Performance: Scripts written in PHP usually execute or runs faster than those
written in other scripting languages like ASP, Ruby, Python, Java, etc.
Vast Community: Since PHP is supported by the worldwide community, finding
help or documentation related to PHP online is extremely easy.

Getting Started with PHP


Before begin, be sure to have a code editor and some working knowledge of HTML and CSS.

Setting Up a Local Web Server


PHP script execute on a web server running PHP. So before you start writing any PHP
program you need the following program installed on your computer.

The Apache Web server


The PHP engine
The MySQL database server

You can either install them individually or choose a pre-configured package for your
operating system like Linux and Windows. Popular pre-configured package are XAMPP and
WampServer.

XAMPP is a Windows web development environment. It allows you to create web


applications with Apache, PHP and a MySQL database. It will also provide the MySQL
administrative tool PhpMyAdmin to easily manage your databases using a web browser.The
official website for downloading and installation instructions for the
XAMPP: https://www.apachefriends.org/

Basic PHP Syntax


A PHP script starts with the <?php and ends with the ?> tag.
The PHP delimiter <?php and ?> in the following example simply tells the PHP
engine to treat the enclosed code block as PHP code, rather than simple HTML.
<?php
// PHP code goes here
?>
…………………………………………………………………………………………………..
Lecture 17 Web Systems and Technologies

<?php

// Some code to be executed

echo "Hello, world!";

?>

Creating Your First PHP Script


<!DOCTYPE html>
<html>
<body>

<h1>My first PHP page</h1>

<?php
echo "Hello World!";
?>

</body>
</html>

Note: PHP statements end with a semicolon (;).

PHP Comments
A comment is simply text that is ignored by the PHP engine. The purpose of comments is to
make the code more readable. It may help other developer (or you in the future when you edit
the source code) to understand what you were trying to do with the PHP.

PHP support single-line as well as multi-line comments. To write a single-line comment


either start the line with either two slashes (//) or a hash symbol (#). For example:

<?php

// This is a single line comment

# This is also a single line comment

echo "Hello, world!";

?>

However to write multi-line comments, start the comment with a slash

followed by an asterisk (/*) and end the comment with an asterisk followed by
a slash (*/), like this:
Lecture 17 Web Systems and Technologies

<?php

/* This is a multiple line comment block that spans across more than one
line */

echo "Hello, world!";

?>

Case Sensitivity in PHP


Variable names in PHP are case-sensitive.

As a result the variables $color, $Color and $COLOR are treated as three different
variables.

<?php
// Assign value to variable
$color = "blue";
// Try to print variable value
echo "The color of the sky is " . $color . "<br>";
echo "The color of the sky is " . $Color . "<br>";
echo "The color of the sky is " . $COLOR . "<br>";
?>
NOTE: However, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-
defined functions are case-insensitive.

PHP Variables
Variables are used to store data, like string of text, numbers, etc. Variable values can change
over the course of a script. Here're some important things to know about variables:

In PHP, a variable does not need to be declared before adding a value to it. PHP
automatically converts the variable to the correct data type, depending on its value.
After declaring a variable it can be reused throughout the code.
The assignment operator (=) used to assign value to a variable.

In PHP variable can be declared as: $var_name = value;

<?php

// Declaring variables

$txt = "Hello World!";


Lecture 17 Web Systems and Technologies

$number = 10;

// Displaying variables value

echo $txt; // Output: Hello World!

echo $number; // Output: 10

?>

Naming Conventions for PHP Variables


These are the following rules for naming a PHP variable:

All variables in PHP start with a $ sign, followed by the name of the variable.
A variable name must start with a letter or the underscore character _.
A variable name cannot start with a number.
A variable name in PHP can only contain alpha-numeric characters and underscores (A-
z, 0-9, and _).
A variable name cannot contain spaces.

NOTE: Variable names in PHP are case sensitive, it means $x and $X are two different
variables. So be careful while defining variable names.

The PHP echo Statement


The echo statement can output one or more strings. In general terms, the echo statement can
display anything that can be displayed to the browser, such as string, numbers, variables
values, the results of expressions etc.

Since echo is a language construct not actually a function (like if statement), you can use it
without parentheses e.g. echo or echo(). However, if you want to pass more than one
parameter to echo, the parameters must not be enclosed within parentheses.

Display Strings of Text


The following example will show you how to display a string of text with the
echo statement:
<?php
// Displaying string of text
echo "Hello World!";
?>
Lecture 17 Web Systems and Technologies

Display HTML Code


The following example will show you how to display HTML code using the echo statement:
<?php
// Displaying HTML code
echo "<h4>This is a simple heading.</h4>";
echo "<h4 style='color: red;'>This is heading with style.</h4>";

Display Variables
The following example will show you how to display variable using the echo statement:
<?php
// Defining variables
$txt = "Hello World!";
$num = 123456789;
$colors = array("Red", "Green", "Blue");
// Displaying variables
echo $txt;
echo "<br>";
echo $num;
echo "<br>";
echo $colors[0];
?>

You might also like