PHP Unit1
PHP Unit1
Unit1:
Introducing PHP – Basic development Concepts – Creating first PHP Scripts –
Using Variable and Operators – Storing Data in variable – Understanding Data
types – Setting and Checking Variables – Data types – Using Constants –
Manipulating Variables with Operators.
What is PHP
PHP is an open-source, interpreted, and object-oriented scripting language that can be
executed at the server-side. PHP is well suited for web development. Therefore, it is used
to develop web applications (an application that executes on the server and generates
the dynamic page.).
PHP was created by Rasmus Lerdorf in 1994 but appeared in the market in 1995. PHP
7.4.0 is the latest version of PHP, which was released on 28 November. Some important
points need to be noticed about PHP are as followed:
617.1K
8. Modules and PHP Abstract class | Build a CMS using OOP PHP CMS tutorial MVC [2020]
o It handles dynamic content, database as well as session tracking for the website.
o You can create sessions in PHP.
o It can access cookies variable and also set cookies.
o It helps to encrypt the data and apply validation.
o PHP supports several protocols such as HTTP, POP3, SNMP, LDAP, IMAP, and
many more.
o Using PHP language, you can control the user to access some pages of your
website.
o As PHP is easy to install and set up, this is the main reason why PHP is the best
language to learn.
o PHP can handle the forms, such as - collect the data from users using forms, save
it into the database, and return useful information to the user. For example -
Registration form.
PHP Features
PHP is very popular language because of its simplicity and open source. There are some
important features of PHP given below:
Performance:
PHP script is executed much faster than those scripts which are written in other
languages such as JSP and ASP. PHP uses its own memory, so the server workload and
loading time is automatically reduced, which results in faster processing speed and
better performance.
Open Source:
PHP source code and software are freely available on the web. You can develop all the
versions of PHP according to your requirement without paying any cost. All its
components are free to download and use.
PHP has easily understandable syntax. Programmers are comfortable coding with it.
Embedded:
PHP code can be easily embedded within HTML tags and script.
Platform Independent:
PHP is available for WINDOWS, MAC, LINUX & UNIX operating system. A PHP
application developed in one OS can be easily executed in other OS also.
Database Support:
PHP supports all the leading databases such as MySQL, SQLite, ODBC, etc.
Error Reporting -
PHP has predefined error reporting constants to generate an error notice or warning at
runtime. E.g., E_ERROR, E_WARNING, E_STRICT, E_PARSE.
PHP allows us to use a variable without declaring its datatype. It will be taken
automatically at the time of execution based on the type of data it contains on its value.
PHP is compatible with almost all local servers used today like Apache, Netscape,
Microsoft IIS, etc.
Security:
PHP is a secure language to develop the website. It consists of multiple layers of security
to prevent threads and malicious attacks.
Control:
Different programming languages require long script or code, whereas PHP can do the
same work in a few lines of code. It has maximum control over the websites like you can
make changes easily whenever you want.
Web Development
PHP is widely used in web development nowadays. PHP can develop dynamic websites
easily. But you must have the basic the knowledge of following technologies for web
development as well.
o HTML
o CSS
o JavaScript
o Ajax
o XML and JSON
o jQuery
Prerequisite
Before learning PHP, you must have the basic knowledge of HTML,
CSS, and JavaScript. So, learn these technologies for better implementation of PHP.
CSS - CSS helps to make the webpage content more effective and attractive.
All the php code is written inside php code tags which is <?php and ?>.
And the file with php code is saved with an extension .php. Here is a
simple example:
<?php
// code statements
?>
<?php
?>
output
Hello, World!
<html>
<body>
<?php
?>
</body>
</html
Below we have a listed down the main syntax rules that you must
follow while writing php code.
1. All the php code in a php script should be enclosed within <?
php and ?>, else it will not be considered as php code. Adding
php code inside the PHP tags is known as Escaping to php.
7.
?>
/*
*/
?>
But all the predefined keywords and functions like if, else, echo etc
are case insensitive.
<?php
?>
Hello, World!
Hello, World!
14. {
17. }
?>
PHP Echo
PHP echo is a language construct, not a function. Therefore, you don't need to use
parenthesis with it. But if you want to use more than one parameter, it is required to use
parenthesis.
PHP echo statement can be used to print the string, multi-line strings, escaping
characters, variable, array, etc. Some important points that you must know about the
echo statement are:
20.1M
409
History of Java
Next
Stay
1. <?php
2. echo "Hello by PHP echo";
3. ?>
Output:
1. <?php
2. echo "Hello by PHP echo
3. this is multi line
4. text printed by
5. PHP echo statement
6. ";
7. ?>
Output:
Hello by PHP echo this is multi line text printed by PHP echo statement
1. <?php
2. echo "Hello escape \"sequence\" characters";
3. ?>
Output:
1. <?php
2. $msg="Hello JavaTpoint PHP";
3. echo "Message is: $msg";
4. ?>
Output:
PHP Print
Like PHP echo, PHP print is a language construct, so you don't need to use parenthesis
with the argument list. Print statement can be used with or without parentheses: print
and print(). Unlike echo, it always returns 1.
PHP print statement can be used to print the string, multi-line strings, escaping
characters, variable, array, etc. Some important points that you must know about the
echo statement are:
o print is a statement, used as an alternative to echo at many times to display the output.
o print can be used with or without parentheses.
o print always returns an integer value, which is 1.
o Using print, we cannot pass multiple arguments.
o print is slower than the echo statement.
30.4M
681
1. <?php
2. print "Hello by PHP print ";
3. print ("Hello by PHP print()");
4. ?>
Output:
1. <?php
2. print "Hello by PHP print
3. this is multi line
4. text printed by
5. PHP print statement
6. ";
7. ?>
Output:
Hello by PHP print this is multi line text printed by PHP print statement
1. <?php
2. print "Hello escape \"sequence\" characters by PHP print";
3. ?>
Output:
1. <?php
2. $msg="Hello print() in PHP";
3. print "Message is: $msg";
4. ?>
Output:
PHP Variable
Use of variables
Variable type casting
PHP Constant
PHP Operators
Arithmetic operators
Assignment Operators
Comparison operators
Logical operators
PHP is a loosely typed language; it does not have explicit defined data types.
PHP determines the data types by analyzing the attributes of data supplied.
PHP implicitly supports the following data types
Floating point number – decimal numbers e.g. 3.14. they are also
known as double or real numbers. The maximum value of a float is
platform-dependent. Floating point numbers are larger than integers.
Character string – e.g. Hello World
Boolean – e.g. True or false.
Before we go into more details discussing PHP data types, let’s first discuss
variables.
PHP Variable
A variable is a name given to a memory location that stores data at runtime.
PHP – Variables
Scope can be defined as the range of availability a variable has to the program in which it is
declared. PHP variables can be one of four scope types:
Local variables
Function parameters
Global variables
Static variables
assignx();
print "\$x outside of function is $x. ";
?>
This will produce the following result.
$x inside function is 0.
$x outside of function is 4.
Think of a variable as a glass containing water. You can add water into the
glass, drink all of it, refill it again etc.
Variables are used to store data and provide stored data when needed. Just
like in other programming languages, PHP supports variables too. Let’s now
look at the rules followed when creating variables in PHP.
All variable names must start with the dollar sign e.g.
Variable names are case sensitive; this means $my_var is different from
$MY_VAR
All variables names must start with a letter follow other characters e.g.
$my_var1. $1my_var is not a legal variable name.
Variable names must not contain any spaces, “$first name” is not a legal
variable name. You can instead use an underscore in place of the
space e.g. $first_name. You cant use characters such as the dollar or
minus sign to separate variable names.
Let’s now look at how PHP determines the data type depending on the
attributes of the supplied data.
<?php
$my_var = 1;
echo $my_var;
?>
Output:
1
Floating point numbers
<?php
$my_var = 3.14;
echo $my_var;
?>
Output:
3.14
Character strings
<?php
$my_var ="Hypertext Pre Processor";
echo $my_var;
?>
Output:
Hypertext Pre Processor
The $$var (double dollar) is a reference variable that stores the value of the $variable inside it.
Example 1
<?php
$x = "abc";
$$x = 200;
echo $x."<br/>";
echo $$x."<br/>";
echo $abc;
?>
Output:
Example2
1. <?php
2. $x="U.P";
3. $$x="Lucknow";
4. echo $x. "<br>";
5. echo $$x. "<br>";
6. echo "Capital of $x is " . $$x;
7. ?>
Output:
xample3
1. <?php
2. $name="Cat";
3. ${$name}="Dog";
4. ${${$name}}="Monkey";
5. echo $name. "<br>";
6. echo ${$name}. "<br>";
7. echo $Cat. "<br>";
8. echo ${${$name}}. "<br>";
9. echo $Dog. "<br>";
10. ?>
Output:
In the above example, we have assigned a value to the variable name Cat. Value of reference variable ${$name} is assigned
as Dog and ${${$name}} as Monkey.
Now we have printed the values as $name, ${$name}, $Cat, ${${$name}} and $Dog.
Use of Variables
Variables help separate data from the program algorithms.
The same algorithm can be used for different input data values.
For example, suppose that you are developing a calculator program that adds
up two numbers, you can create two variables that accept the numbers then
you use the variables names in the expression that does the addition.
In other languages such as C#, you have to cast the variables. The code
below shows type casting in C#.
PHP Constant
Define constant– A constant is a variable whose value cannot be changed at
runtime.
Suppose we are developing a program that uses the value of PI 3.14, we can
use a constant to store its value.
PHP Operators
Arithmetic operators
Arithmetic operators are used to perform arithmetic operations on numeric
data. The concatenate operator works on strings values too. PHP supports
the following operators.
PHP Comments
PHP comments can be used to describe any line of code so that other developer can
understand the code easily. It can also be used to hide any code.
PHP supports single line and multi line comments. These comments are similar to C/C+
+ and Perl style (Unix shell style) comments.
1. <?php
2. // this is C++ style single line comment
3. # this is Unix Shell style single line comment
4. echo "Welcome to PHP single line comments";
5. ?>
Output:
30.5M
529
Hello Java Program for Beginners
1. <?php
2. /*
3. Anything placed
4. within comment
5. will not be displayed
6. on the browser;
7. */
8. echo "Welcome to PHP multi line comment";
9. ?>
Output:
Summary
PHP is a loosely typed language.
Variables are memory locations used to store data
The value of constants cannot be changed at runtime
Type casting is used to convert a value or variable into a desired data
type
Arithmetic operators are used to manipulate numeric data
Assignment operators are used to assign data to variables
Comparison operators are used to compare variables or values
Logical operators are used to compare conditions or values