Document Details

HallowedLawrencium

Uploaded by HallowedLawrencium

Tags

PHP programming web development scripting language programming

Summary

This document provides an introduction to PHP, a server-side scripting language. It details its characteristics and various versions. The document also mentions different applications of PHP, such as web development, and touches upon its features like handling forms and databases.

Full Transcript

Unit - I Introduction to PHP Contents: Evaluation of PHP, Language Basics, Defining variable and constant, Data type Operator and Expression, Decisions & Loops, Capturing Form Data, Dealing with Multi-value filed, Generating File uploaded form, Redirecting a form after sub...

Unit - I Introduction to PHP Contents: Evaluation of PHP, Language Basics, Defining variable and constant, Data type Operator and Expression, Decisions & Loops, Capturing Form Data, Dealing with Multi-value filed, Generating File uploaded form, Redirecting a form after submission  Evaluation of PHP 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.  The term PHP is an acronym for – Hypertext Preprocessor.  PHP is a server-side scripting language designed specifically for web development.  It is open-source which means it is free to download and use.  It is very simple to learn and use.  The file extension of PHP is “.php”. 1 Characteristics of PHP  PHP code is executed in the server.  It can be integrated with many databases such as Oracle, Microsoft SQL Server, MySQL, PostgreSQL, Sybase, and Informix.  It is powerful to hold a content management system like WordPress and can be used to control user access.  It supports main protocols like HTTP Basic, HTTP Digest, IMAP, FTP, and others.  One of the main reasons behind this is that PHP can be easily embedded in HTML files and HTML codes can also be written in a PHP file.  The thing that differentiates PHP from the client-side language like HTML is, that PHP codes are executed on the server whereas HTML codes are directly rendered on the browser.  PHP codes are first executed on the server and then the result is returned to the browser.  The only information that the client or browser knows is the result returned after executing the PHP script on the server and not the actual PHP codes present in the PHP file.  Also, PHP files can support other client-side scripting languages like CSS and JavaScript.  PHP performs system functions. It can create, open, read, write, and close the files.  PHP can handle forms. It can gather data from files, save data to a file, through email you can send data, return data to the user.  You add, delete, modify elements within your database through PHP.  Access cookies variables and set cookies.  Using PHP, you can restrict users to access some pages of your website.  It can encrypt data. 2 PHP Versions  PHP was developed by Rasmus Lerdorf in 1994 as a simple set of CGI binaries written in C. He called this suite of scripts "Personal Home Page Tools". It can be regarded as PHP version 1.0.  In April 1996, Rasmus introduced PHP/FI. Included built-in support for DBM, mSQL, and Postgres95 databases, cookies, user-defined function support. PHP/FI was given the version 2.0 status.  PHP: Hypertext Preprocessor – PHP 3.0 version came about when Zeev Suraski and Andi Gutmans rewrote the PHP parser and acquired the present-day acronym. It provided a mature interface for multiple databases, protocols and APIs, object-oriented programming support, and consistent language syntax.  PHP 4.0 was released in May 2000 powered by Zend Engine. It had support for many web servers, HTTP sessions, output buffering, secure ways of handling user input and several new language constructs.  PHP 5.0 was released in July 2004. It is mainly driven by its core, the Zend Engine 2.0 with a new object model and dozens of other new features. PHP's development team includes dozens of developers and others working on PHP- related and supporting projects such as PEAR, PECL, and documentation.  PHP 7.0 was released in Dec 2015. This was originally dubbed PHP next generation (phpng). Developers reworked Zend Engine is called Zend Engine 3. Some of the important features of PHP 7 include its improved performance, reduced memory usage, Return and Scalar Type Declarations and Anonymous Classes.  PHP 8.0 was released on 26 November 2020. This is a major version having many significant improvements from its previous versions. One standout feature is Just-in-time compilation (JIT) that can provide substantial performance improvements. The latest version of PHP is 8.2.8, released on July 4th, 2023. PHP Application Areas  PHP is a server-side scripting language that is embedded in HTML. It is used to manage dynamic content, databases, session tracking, even build entire e- 3 commerce sites. Although it is especially suited to web development, you can also build desktop standalone applications as PHP also has a command-line interface. You can use PHP-GTK extension to build GUI applications in PHP.  PHP is widely used for building web applications, but you are not limited to output only HTML. PHP's ouput abilities include rich file types, such as images or PDF files, encrypting data, and sending emails. You can also output easily any text, such as JSON or XML.  PHP is a cross-platform language, capable of running on all major operating system platforms and with most of the web server programs such as Apache. Why use PHP  PHP is a server-side scripting language, which is used to design the dynamic web applications with MySQL database.  It handles dynamic content, database as well as session tracking for the website.  You can create sessions in PHP.  It can access cookies variable and also set cookies.  It helps to encrypt the data and apply validation.  PHP supports several protocols such as HTTP, POP3, SNMP, LDAP, IMAP, and many more.  Using PHP language, you can control the user to access some pages of your website.  As PHP is easy to install and set up, this is the main reason why PHP is the best language to learn.  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. 4 How to Install PHP on Windows 10 or 11? What Do I Need? ◦ To start using PHP, you can: ◦ Find a web host with PHP and MySQL support ◦ Install a web server on your own PC, and then install PHP and MySQL Use a Web Host With PHP Support ◦ If your server has activated support for PHP you do not need to do anything. ◦ Just create some.php files, place them in your web directory, and the server will automatically parse them for you. ◦ You do not need to compile anything or install any extra tools. ◦ Because PHP is free, most web hosts offer PHP support. Set Up PHP on Your Own PC ◦ However, if your server does not support PHP, you must: 5 ◦ install a web server ◦ install PHP ◦ install a database, such as MySQL The official PHP website (PHP.net) has installation instructions for PHP: http://php.net/manual/en/install.php OR Install XAMPP on your system.  Language Basics  PHP is primarily used for server-side web development. It enables the creation of dynamic web pages by embedding PHP code within HTML.  PHP can perform various tasks, including handling form data, generating dynamic page content, managing databases, and interacting with servers. Syntax : PHP Hello World Features of PHP  Dynamic Typing: PHP is dynamically typed, meaning you don’t need to declare the data type of a variable explicitly.  Cross-Platform: PHP runs on various platforms, making it compatible with different operating systems.  Database Integration: PHP provides built-in support for interacting with 6 databases, such as MySQL, PostgreSQL, and others.  Server-Side Scripting: PHP scripts are executed on the server, generating HTML that is sent to the client’s browser. Advantages of PHP  Open Source: PHP is an open-source language, making it freely available for use and distribution. This encourages a large community of developers, contributing to its growth and improvement.  Easy to Learn: PHP syntax is similar to C and other programming languages, making it relatively easy for developers to learn, especially for those with a background in programming.  Web Integration: PHP is designed specifically for web development and is embedded within HTML. It seamlessly integrates with various web technologies, facilitating the creation of dynamic and interactive web pages.  Database Support: PHP has excellent support for various databases, including MySQL, PostgreSQL, SQLite, and more. This makes it easy to connect and interact with databases, a crucial aspect of many web applications.  Cross-Platform Compatibility: PHP is platform-independent and runs on various operating systems, including Windows, Linux, macOS, and others. This ensures compatibility across different environments.  Large Community and Documentation: PHP has a vast and active community of developers. The abundance of online resources, tutorials, and documentation makes it easier for developers to find solutions and seek help when needed.  Frameworks and CMS: There are popular PHP frameworks like Laravel, Symfony, and CodeIgniter, which provide pre-built modules and features, aiding in rapid development. Additionally, PHP supports widely used content management systems (CMS) like WordPress and Joomla.  Server-Side Scripting: PHP scripts are executed on the server, reducing the load on the client’s side. This server-side scripting capability is crucial for generating dynamic content and performing server-related tasks.  Community Support: The PHP community actively contributes to the language’s development, ensuring regular updates, security patches, and improvements. 7 Disadvantages of PHP  Inconsistency: PHP has been criticized for inconsistencies in function names and parameter orders. This can lead to confusion for developers, especially when working with a mix of older and newer functions.  Security Concerns: If not handled properly, PHP code may be susceptible to security vulnerabilities, such as SQL injection and cross-site scripting (XSS). Developers need to be cautious and follow best practices to secure PHP applications.  Performance: While PHP performs well for many web applications, it may not be as fast as some compiled languages like C or Java. However, advancements and optimizations in recent versions have improved performance.  Lack of Modern Features: Compared to newer languages, PHP may lack some modern language features. However, recent versions of PHP have introduced improvements and features to address this concern.  Scalability Challenges: PHP can face challenges when it comes to scaling large and complex applications. Developers may need to adopt additional tools or frameworks to address scalability issues.  Not Suitable for Large-Scale Applications: While PHP is suitable for small to medium-sized projects, it might not be the best choice for extremely large and complex applications where more structured languages might be preferred.  Limited Object-Oriented Programming (OOP) Support: Although PHP supports OOP, its implementation has been criticized for not being as robust as in some other languages. However, recent versions have introduced improvements to enhance OOP capabilities.  Defining variable and constant PHP Constant: ◦ PHP Constants are the identifiers that remain the same. Usually, it does not change during the execution of the script. ◦ PHP constants are name or identifier that can't be changed during the execution of the script except for magic constants, which are not really constants. ◦ They are case-sensitive. By default, constant identifiers are always uppercase. 8 Usually, a Constant name starts with an underscore or a letter which is followed by a number of letters and numbers. ◦ They are no need to write a constant with the $ sign. The constant() function is used to return the value of a constant. ◦ The constant() function is used to return the value of a constant. ◦ Constants are either identifiers or simple names that can be assigned any fixed values. ◦ They are similar to a variable except that they can never be changed. ◦ They remain constant throughout the program and cannot be altered during execution. ◦ Once a constant is defined, it cannot be undefined or redefined. ◦ Constant identifiers should be written in upper case following the convention. ◦ By default, a constant is always case-sensitive, unless mentioned. ◦ A constant name must never start with a number. It always starts with a letter or underscores, followed by letter, numbers or underscore. It should not contain any special characters except underscore. ◦ PHP constants can be defined by 2 ways: 1. Using define() function 2. Using const keyword ◦ ◦ Creating a PHP Constant ◦ The define() function in PHP is used to create a constant as shown below: Syntax: define(name, value, case_insensitive) The parameters are as follows:  name: The name of the constant. 9  value: The value to be stored in the constant.  case_insensitive: Defines whether a constant is case insensitive. By default this value is False, i.e., case sensitive. Example: constant() function Instead of using the echo statement ,there is an another way to print constants using the constant() function. Syntax: constant(name) Example: Constants are Global: 10 By default, constants are automatically global, and can be used throughout the script, accessible inside and outside of any function. Example: PHP Variable: ◦ Variable in any programming language is a name given to a memory location thatholds a value. ◦ You can say that variables are containers for any type of values. ◦ There are some rules to write variable names in PHP. Rules for variable names:  Variable names in PHP start with a dollar ($) sign followed by the variable name.  Variable name can contain alphanumeric characters and underscore (_).  Variable names must start with a letter or an underscore (_). (For eg: $abc, $x1, $_g, $abc_1 etc.)  Variable names cannot start with a number.  Variable names are case-sensitive. (for eg: $x and $X are treated as two different variables.)  In PHP we don’t use any command to declare variables.  A variable is created as soon as you assign a value to it.  A variable takes a datatype according to the value assigned to it.  Since we don’t have to specify datatypes for PHP variables, PHP is called as loosely typed language. 11 Example: Variable Scopes  Scope of a variable is defined as its extent in a program within which it can be accessed, i.e. the scope of a variable is the portion of the program within which it is visible or can be accessed.  Depending on the scopes, PHP has three variable scopes: ▪ Local variables ◦ The variables declared within a function are called local variables to that function and have their scope only in that particular function. ◦ In simple words, it cannot be accessed outside that function. Any declaration of a variable outside the function with the same name as that of the one within the function is a completely different variable. Example: This example shows the local variable in PHP. ▪ Global Variables ◦ The variables declared outside a function are called global variables. ◦ These variables can be accessed directly outside a function. ◦ To get access within a function we need to use the “global” keyword before the variable to refer to the global variable. Example: This example shows the Global Variables in PHP. ▪ Static variable ◦ It is the characteristic of PHP to delete the variable, once it completes its execution and the memory is freed. ◦ But sometimes we need to store the variables even after the completion of function execution. ◦ To do this we use the static keywords and the variables are then called static variables. 13 ◦ PHP associates a data type depending on the value for the variable. Example: This example shows the Static variable in PHP. Data types used by PHP to declare or construct variables:  Doubles  Integers  NULL  Strings  Booleans  Arrays  Objects  Resources ▪ Variable Variables  PHP allows us to use dynamic variable names, called variable variables.  Variable variables are simply variables whose names are dynamically created by another variable’s value. Example: This example shows the declaration of a variable by the use of the another variable. 14 Difference between PHP Constant & PHP Variables PHP Constants PHP Variables In PHP constants there is no need to use In PHP Variables the $ sign is been used. $ sign. The data type of PHP constant cannot be The data type of the PHP variable can be changed during the execution of the changed during the execution of the script. script. A PHP constant once defined cannot be A PHP variable can be undefined as well as redefined. can be redefined. We can not define a constant using any simple assignment operator rather it can We can define a variable using a simple only be defined using define(). assignment operation(=). Usually, constants are written in On the other hand, variables are written in numbers. letters and symbols. PHP constants are automatically global PHP variables are not automatically global across the entire script. in the entire script. PHP constant is comparatively slower A PHP variable is comparatively faster than than PHP variable the PHP constant once defined can never be undefined variable can be easily undefined. 15  Data type, Operator and Expression PHP Data Type : Data Types define the type of data a variable can store. PHP allows eight different types of data types. All of them are discussed below. There are pre-defined, user-defined, and special data types. The predefined data types are:  Boolean:  Boolean data types are used in conditional testing. Hold only two values, either TRUE(1) or FALSE(0).  Successful events will return true and unsuccessful events return false. NULL type values are also treated as false in Boolean.  Apart from NULL, 0 is also considered false in boolean.  If a string is empty then it is also considered false in boolean data type. Example:  Integer :  Integers hold only whole numbers including positive and negative numbers, i.e., numbers without fractional part or decimal point.  They can be decimal (base 10), octal (base 8), or hexadecimal (base 16). The default base is decimal (base 10).  The octal integers can be declared with leading 0 and the hexadecimal can be declared with leading 0x.  The range of integers must lie between -2^31 to 2^31.  An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647 Example: 16  Double:  Can hold numbers containing fractional or decimal parts including positive and negative numbers or a number in exponential form.  By default, the variables add a minimum number of decimal places.  The Double data type is the same as a float as floating-point numbers or real numbers. Example:  String  Hold letters or any alphabets, even numbers are included. 17  These are written within double quotes during declaration.  The strings can also be written within single quotes, but they will be treated differently while printing variables. Example: The user-defined (compound) data types are:  Array:  Array is a compound data type that can store multiple values of the same data type.  Below is an example of an array of integers. It combines a series of data that are related together. Example:  Objects:  Classes and objects are the two main aspects of object-oriented programming. 18  A class is a template for objects, and an object is an instance of a class.  When the individual objects are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.  Objects are defined as instances of user-defined classes that can hold both values and functions and information for data processing specific to the class.  This is an advanced topic and will be discussed in detail in further articles.  When the objects are created, they inherit all the properties and behaviours from the class, having different values for all the properties.  Objects are explicitly declared and created from the new keyword. Example: The special data types are:  NULL: 19  These are special types of variables that can hold only one value i.e., NULL.  We follow the convention of writing it in capital form, but it’s case- insensitive NULL, Null, and null are treated the same.  If a variable is created without a value or no value, it is automatically assigned a value of NULL. It is written in capital letters. Example:  Resource:  Resources in PHP are not an exact data type.  These are basically used to store references to some function call or to external PHP resources.  For example, consider a database call. This is an external resource.  Resource variables hold special handles for files and database connections. PHP Operators :  Operators are used to performing operations on some values. In other words, we can describe operators as something that takes some values, performs some operation on them, and gives a result.  Just like any other programming language, PHP also supports various types of operations like arithmetic operations(addition, subtraction, etc), logical operations(AND, OR etc), Increment/Decrement Operations, etc.  Thus, PHP provides us with many operators to perform such operations on various operands or variables, or values. 20  These operators are nothing but symbols needed to perform operations of various types. Given below are the various groups of operators: Arithmetic Operators Operator Name Example Explanation + Addition $a + $b Sum of operands - Subtraction $a - $b Difference of operands * Multiplication $a * $b Product of operands / Division $a / $b Quotient of operands % Modulus $a % $b Remainder of operands ** Exponentiation $a ** $b $a raised to the power $b Example: 21 Logical or Relational Operators:  In PHP, logical operators are used to combine conditional statements. These operators allow you to create more complex conditions by combining multiple conditions together.  Logical operators are generally used in conditional statements such as if, while, and for loops to control the flow of program execution based on specific conditions.  The following table highligts the logical operators that are supported by PHP. Operator Name Example Explanation Return TRUE if both $a and And $a and $b and $b are true Return TRUE if either Or Or $a or $b $a or $b is true Return TRUE if either $ xor Xor $a xor $b or $b is true but not both Return TRUE if $a is ! Not ! $a not true Return TRUE if either && And $a && $b $a and $b are true Return TRUE if either || Or $a || $b $a or $b is true Example:

Use Quizgecko on...
Browser
Browser