View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

PHP Tutorial: Learn from Basics

By Pavan Vadapalli

Updated on Nov 14, 2024 | 6 min read | 5.6k views

Share:

PHP(PHP: HyperText Processor) is a free, open-source scripting language for the server-side, widely used in web development. It is also a general-purpose programming language developers can use for creating diverse applications.

PHP is a scripting language integrated with an HTML script and handles dynamic web pages, session tracking, databases, and other features.

Check out our free courses related to software development.

Installing PHP on your Computer

PHP is available at https://www.php.net/downloads in its most recent version.

Alternatively, you can get the PHP.msi package, which is the Windows installer, from sourceforge.net

Writing your First Program in PHP

The first program practising program in every new programming language is printing ‘Hello World’. We shall see the code for the same below:

<html>

<body>

<h1>PHP page</h1>

<?php

echo “Hello World!”;

?>

</body>

</html>

Variable and Data Types in PHP

A few facts about variables in PHP are mentioned below:

  • In PHP, all variables are marked with a dollar symbol ($) at the beginning of their declaration.
  • The ‘=’ operator is used to assign variables to expressions.
  • Variables in PHP do not have inherent or intrinsic data types. A variable cannot know its usage to hold an integer or a string until its utilisation.

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.

Data Types in PHP

Integer

They are whole numbers without a decimal point, such as 5625. Integers can be both positive and negative. Integers may be allocated to variables or utilised in expressions.

Syntax:

$variable = 1234567;

$another_variable = -1234567 + 1234567;

Double

A double value represents a decimal number. It can either be positive or negative.

Syntax:

$doub_var = 1.345679;

Boolean

A boolean variable has two values, TRUE or FALSE.

NULL

A null data type may have only one value, i.e. NULL.

Syntax:

$variable_n = NULL;

String

A String data type represents a sequence or set of characters. Strings can either be written inside single or double quotes.

Syntax:

$string_s = “This is a String”;

Coverage of AWS, Microsoft Azure and GCP services

Certification8 Months

Job-Linked Program

Bootcamp36 Weeks

Constants in PHP

A constant is a variable or an identifier that refers to a single, static value,  which cannot change during script execution. Therefore, modifying or undefining a constant after it has been defined is impossible. Constant identifiers are usually written in uppercase as a matter of convention. 

Syntax:

constant() // This function is used to return the value of a constant

Operators in PHP

Let us consider the expression a + b. In this expression, ‘a’ and ‘b’ are operands while ’+’ is the operator. 

PHP supported operators are:

  • Arithmetic Operators
  • Operators for Comparison
  • Relational Operators
  • Assignment or Short-Hand Operators
  • Conditional Operators

We shall only look into the first two types of operators for PHP basics- Arithmetic and Comparison operators.

Arithmetic Operators

Example: X = 200 , Y = 100

(+): This is the arithmetic addition operator; X + Y will produce 300 as a result.

(-): This is the arithmetic subtraction operator; X – Y will produce 100 as a result.

(*): This is the arithmetic product operator; X * Y will produce 20000 as a result.

(/):  This is the arithmetic division operator; X / Y will produce two as a result.

(%):  This is the arithmetic modulus operator, X % Y will produce 0 as a result. 

(++): This is the arithmetic increment operator; X++ will produce 201 as a result.

(–): This is the arithmetic decrement operator; X– will produce 199 as a result.

Comparison Operators

Example: P = 10 , Q = 20

(==): This operator checks if the value of one operand is equal to the other or not and returns TRUE or FALSE; P == Q will produce FALSE as the output.

(!= ): This operator is just the opposite of the ‘==’ operator. It checks if the value of an operand is not equal to the other and returns TRUE or FALSE; P != Q will produce TRUE as the output.

(>): This operator checks if the value of the left-hand operator is greater than the right-hand operator or not;  P > Q will return FALSE as the output.

(<): This operator checks if the value of the right-hand operator is greater than the left-hand operator or not;  P < Q will return TRUE as the output.

(>=): This operator checks if the value of the left-hand operator is greater than or equal to the right-hand operator or not.(<=): This operator checks if the value of the right-hand operator is greater than or equal to the left-hand operator or not.

Arrays in PHP

An array is a data structure that holds one or more items of a similar type in a single variable of the same name.

There are three types of arrays, with each array value accessible through an array index:

  • An array with a numeric index is called a numeric array. It is a contiguous memory allocation where users store and retrieve values using their indexes.
  • An array with strings as indexes is known as an associative array. Instead of storing element values in contiguous index order, this method saves element values in conjunction with key values.
  • A multidimensional array contains a single or more than one array and values accessible by using various indices in different directions.

Advantages of using PHP

PHP’s widespread use is a natural outcome of its multiple benefits, making it a robust and productive scripting platform. A few advantages of PHP are mentioned below:

  • Accelerated loading speed: PHP enables websites to load quicker than many other web development tools. On the other hand, reduced loading time is a critical SEO Ranking Feature that helps promote a website. 
  • Open-source software, mostly free of charge: PHP is a free-to-use technology that enables significant cost reductions in the development process. Additionally, the bulk of development tools often used in conjunction with PHP is open-source and freely available.
  • Can be Integrated with HTML:  The extraordinary compatibility between PHP and HTML is due to PHP’s ability to include HTML programming into its code. PHP can be added into the HTML Script with the use of <?php ?> tags.
  • Compatible with Cloud technologies: Cloud computing technologies, such as Amazon Web Services, are used by contemporary goods for various functions. Several cloud services, including AWS Lambda, can handle applications based on PHP.

Conclusion

PHP is backed by vast reference material and a supportive community, making it an accessible and popular programming language for web development. 

upGrad’s Executive Post Graduate Program in Full Stack Development

A Full-Stack Developer is probably the tech-related buzzword of the decade in software and web development. So, kick-start your Full-Stack Developer journey with upGrad’s Full-Stack Development Course.

Here’s what the course offers:

  • The status of an Alumni from IIIT Bangalore
  • More than 7 case studies and projects to choose from
  • upGrad 360° Support for Your Career
  • Career Change Bootcamp for non-technical and new programmers in the software industry
  • Access to Faculty and Industry Leaders from across the world
  • Query assistance is accessible around the clock, seven days a week

Visit upGrad to learn more!

Frequently Asked Questions (FAQs)

1. What is the full form of PHP?

2. How much time does it take for a beginner to learn PHP?

3. What is the eligibility criterion for upGrad’s Full-Stack Developer Course?

Pavan Vadapalli

900 articles published

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

India’s #1 Tech University

Executive PG Certification in AI-Powered Full Stack Development

77%

seats filled

View Program

Top Resources

Recommended Programs

upGrad

AWS | upGrad KnowledgeHut

AWS Certified Solutions Architect - Associate Training (SAA-C03)

69 Cloud Lab Simulations

Certification

32-Hr Training by Dustin Brimberry

upGrad KnowledgeHut

upGrad KnowledgeHut

Angular Training

Hone Skills with Live Projects

Certification

13+ Hrs Instructor-Led Sessions

upGrad

upGrad KnowledgeHut

Full Stack Development Bootcamp - Essential

Job-Linked Program

Bootcamp

36 Weeks