PHP

What is PHP ?
  • It is an acronym for "PHP: Hypertext Preprocessor".
  • It is a widely-used, open source scripting language.
  • PHP scripts are executed on the server.
  • It is free to download and use.

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

History of PHP :
  • PHP was created in 1994 by Rasmus Lerdorf. It was written in the C Programming Language.
  • It is the most widely used server side programming language used to power some of the largest websites online today.
  • It makes creating websites easier since we can use a template system like WordPress does and embed HTML which will help us output web pages. 
  • In one file we can use HTML, CSS, JavaScript and PHP.
  • It also helps us connect to a database like MySQL which makes it very useful.
  • It is also one of the most well documented server side programming languages and we will find an endless amount of tutorials out there to learn how to use.

Features of PHP :
  • Simple
  • Interpreted
  • Flexibility
  • Open source
  • Platform Independent
  • More Faster
  • Simplicity
  • Efficiency

Pro's of PHP :
  • It is very much popular and easy to learn.
  • Large Open Source Software Community.
  • Lot of libraries,packages and frameworks are available for free.
  • It is available for free.

Con's of PHP :
  • It is not secure if we compare to the languages like java and c#.
  • It executes more slowly than most other languages.
  • It requires more resources.
  • It is inconsistent.

Setup and installation : 
  • Go to the official website of php and download the latest version.
       

  • Create the folder in the c drive with name php.


  • Extract the files in the downloaded zip file and copy all the files into the php folder.
     

  • Rename the file php.ini-production to php.ini.
           

  • Open the php.ini file and give the extension directory path and remove the comments for cgi and fast cgi.

  • Go to the windows feature and check the cgi in iis.
  • We can set default document i.e., if we didn't specify the file this file will get executed when we enter localhost.


  • Goto directory browsing and click on enable.


  • Go to fast cgi settings and give the path of php-cgi application.


  • Go to handler mappings and add the module mapping by giving the module name and executable file.


  • Add a website by giving the site name and choose the folder that php files exist.


  • Program to print hello world.
Basic concepts in php :
Variable :
  • In PHP, a variable starts with the $ sign, followed by the name of the variable.
  • Rules for PHP variables : 
    • A variable name must start with a letter or the underscore character.
    • A variable name cannot start with a number.
    • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
    • Variable names are case-sensitive ($age and $AGE are two different variables).

Data Types : 
  • Variables can store data of different types, and different data types can do different things.
  • PHP supports the following data types :
    • String
    • Integer
    • Float 
    • Boolean
    • Array
  • An array stores multiple values in one single variable.

Strings :
  • A PHP string is a sequence of characters i.e. used to store and manipulate text.
  • The strlen() function returns the length of a string.
  • The str_word_count() function counts the number of words in a string.
  • The strrev() function reverses a string.
  • The strpos() function searches for a specific text within a string.
  • The str_replace() function replaces some characters with some other characters in a string.


Conditional Statements : 
Conditional statements are used to perform different actions based on different conditions.
  • if statement executes some code if one condition is true.
  • if...else statement executes some code if a condition is true and another code if that condition is false.
  • if...elseif....else statement executes different codes for more than two conditions.


  • The switch statement is used to perform different actions based on different conditions.
  • Use switch statement to select one of many blocks of code to be executed.


  • while loops through a block of code as long as the specified condition is true.
  • do...while loops through a block of code once, and then repeats the loop as long as the specified condition is true.
  • for loops through a block of code a specified number of times. It is used when we know in advance how many times the script should run.
  • foreach loops through a block of code for each element in an array.

Functions : 
  • PHP function is a piece of code that can be reused many times. 
  • It can take input as argument list and return value. There are thousands of built-in functions in PHP.
  • Besides the built-in PHP functions, we can create our own functions.
  • A function will not execute immediately when a page loads.
  • A function will be executed by a call to the function.

Read from User Input and write to user output syntax :

Write to a file :


Read from a file  :