Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

PHP Server: How to Setup AMPPS and Execute PHP Programs

In this article we will see how to setup a PHP server and study some basics of PHP and execute some simple programs. In this case we will use the AMPPS as PHP Server.

The PHP is an acronym for Hypertext Preprocessor. It is a programming language which allows web developers to create dynamic content that interacts with databases. It is widely used open source scripting language. PHP scripts are executed on the server. PHP is basically used for developing web based software applications.

PHP file contains text, HTML, CSS, JavaScript and PHP code. As PHP scripts are executed on the server, the result is returned as plain HTML in the browser. PHP files have extension “.php”. PHP can send and receive cookies. It can add, delete and modify data in the database.

Why PHP?

  • PHP is server scripting language, so it is used to manage dynamic content, session tracking, databases and e-commerce sites.
  • PHP runs different platforms such as Windows, Linux, Mac OS X etc.
  • It supports wide range of databases such as MySQL, Oracle, Sybase and Microsoft SQL Server.
  • It is open source language and it is free to download and use.
  • It is compatible with almost all servers used today such as Apache, IIS etc.
  • PHP supports large number of protocols such as POP3, IMAP, SNMP,NNTP, HTTP .

Uses of PHP

  • PHP can create, open, read, write, delete and close files on the server.
  • It can upload image to your site , automatically create it, create thumbnail image, resize and make it semi transparent.
  • You can send and receive email by using PHP and you can record who visits your site, who has links to your site etc. It can create a forum for users to visit.
  • You can create desktop applications by using advanced PHP features.
  • You can output images, PDF files, and even flash movies. You can also output any text such as XHTML and XML files.
  • It reduces time to create large websites.
  • It allows creation of shopping carts for e-commerce websites.

How PHP works

PHP is server side scripting which need three things to make this work :

  • PHP parser: It is used to simplify static code analysis and manipulation. To process PHP script, a parser must be installed to generate HTML output that can be sent to the web browser.
  • Database: PHP works with wide range of databases such as MySQL, Oracle , Sybase and Microsoft SQL Server. But most commonly used is freely available MySQL database.
  • Web Server: PHP will work with almost all servers used today such as Apache, Internet Information Server(IIS) etc. But most often used is freely available Apache Server.

Steps for AMPPS PHP server Installation on Windows

First of all, we need to install PHP server. We can use AMPPS, which is a free stack of Apache, MySQL, MongoDB, PHP, Perl & Python for Windows, Linux and Mac. This is created by Softaculous Ltd. a company. The following steps explain the install and setup on windows platform. Follow these steps for installation:

· Once the dowload is complete click on the AMPPS setup. Next Ampps Setup Wizard window will open.

Setup wizard ampps

Figure 1: Setup wizard ampps

  • Click on Next button.
  • The License Agreement window will open and select “I accept the agreement” radio button and click on Next button.

Figure 2: Accept the agreement

  • Click on Next button.

Figure 3: Click next

  • Select the Destination Location to keep Ampps folder and click Next button.

Figure 4: Choose the folder to install

  • Select the place where setup will create program’s shortcut and click on Next button.

Figure 5: Name the folder

  • Select additional tasks like desktop icon, quick launch icon and start menu icon. Check marks the options that you need and click on Next.

Figure 6: Selecting Additional Tasks

  • Click on Install button.

Figure 7: Installing Server

  • The Setup installs AMPPS on your computer.

Figure 8: Installing AMPSS

  • Finally Click on Finish o exit Setup.

Figure 9: Ampps Installed

  • After completing the installation successfully, then you will get following box.

Figure 10: Software installed

Now that the server is installed let us try out a simple example. Before that let us understand the syntax and basics of PHP in the following sections.

In the all programs below we follow these steps to execute the programs:

  • Go in AMPPS folder.
  • Next goto www folder.
  • In www folder either create “test” folder or directly place you program.
  • Next open a broser and access your file/program using the link: http://www.localhost:/

PHP Syntax

A PHP starts with . PHP script can be placed anywhere in the document.

  <?php
  //PHP code here
  ?>
  

For Example:

Following code describes the simple code of PHP, which prints the “Welcome to PHP programming language.

  <html>
  <head>
  <title>PHP Example</title>
  <body>
    <?php 
  echo "Welcome to PHP programming language.";
    ?>
  </body>
  </html>
   

Comments in PHP

A comment in a code specifies that the particular line is not going to execute. It is not read as part of the program. There are two types of comments.

  • Single-line comments
  • Multi-line comments

Single-line comments

Single-line comments are used for short explanations to the local code.

Example :

  <?php
  # It is a single line comment
  // This is also a single line comment
  echo "Hello World!!!!";
  ?>
  

Multi-line comments

Multi-line comments are used for detailed explanations, which are more than one single statement.

Example:

  <?php
  /* It is multiline comment block 
   This gives details information
  Of the local code
  */
  echo "Hello Manisha!!!";
  ?>
  

PHP Case Sensitivity

PHP is case sensitive language. In PHP, all user defined functions, classes and keywords are not case sensitive. For example consider the following:

  <html>
  <body>
  <?php
  echo "Hello World!!!!";
  Echo " Hello World!!!!";
  EcHo " Hello World!!!!";
  ?>
  </body>
  </html>
  

It will produce following output:

  Hello World!!!!
  Hello World!!!!
  Hello World!!!!
  

Variable Types

Variables are containers, which are used to store the information in the middle of the PHP program. In PHP, variables can be declared anywhere in the script. Following are some important things that we all need to know about PHP variables:

  • The variable name is case sensitive.
  • All variables starts with the dollar sign ($).
  • Variables are assigned using the "=" operator.
  • A variable name cannot start with number.
  • In PHP variable, we do not use intrinsic types – it means whether the variable will be used to store a number or a string of characters.
  • In PHP, types are automatically converted from one to another when necessary.
  • A variable name must start with a letter or underscore character.
  • A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, or _ ).

Data types in PHP

A data type is set of values and allows operations on those values. In PHP the programmer doesn’t set types of variable. PHP decides the data type of variables after interpreting the web page. PHP supports eight primitive data types.

  • Integers
  • Doubles
  • Booleans
  • NULL
  • Strings
  • Arrays
  • Objects
  • Resources

Integers

An integer is a whole number that can be positive, negative, or zero. It is number without decimal point. An integer must have at least one digit and it cannot contain comma or blanks. When you are declaring integer variable, you can declare as given below:

integer $variable;

You can also define variable without data type, in such cases PHP will try to determine type of variable based on the value hold by that variable.

For Example: $var=20 ;

Double

A Double is a larger float-type that holds both bigger and more precise numbers. Doubles are used to specify floating-point numbers. When you are declaring double variable, you can declare as given below:

double $variable;

For Example: $var=0.0020522.

You can also define variable without data type , in such cases PHP will try to determine type of variable based on the value hold by that variable.

For Example: $var=88.6 ;

Booleans

Boolean is a data type having only two possible values .It can be either TRUE or FALSE. Any non zero or non empty strings are also considered as TRUE. When you are declaring double variable, you can declare as given below:

boolean $variable;

You can also define variable without data type , in such cases PHP will try to determine type of variable based on the value hold by that variable.

For Example: $a= true;

NULL

When a variable has no value, it considered being NULL. NULL is the only possible value of type NULL and is the case-insensitive keyword NULL. It identifies whether variable is empty or not. You can empty the value of variables by setting the value to NULL.

The NULL variable can be declared as $b=NULL;

Strings

Strings are sequences of characters. It is used to represent text rather than numbers. It can be represented by single quotes or double quotes.

For example:

$str=”hello world!!!”;
  $str1=’hello world!!!’;
  

Arrays

An Array is a compound data type, which contains multiple values in one single variable. If you wanted to add list of items to a variable, you would be creating an array.

For Example:

  <?php
  $players = array( "sachin","ganguly","dravid" );
  echo $players[0]; //prints: first element
  echo "";
  echo $players[1]; //prints: 2
  echo "";
  echo $players[2]; //prints: 3 
  ?>
  

It will produce following output:

  sachin
  ganguly
  dravid
  

Objects

Object variables are owned by each individual object/instance of the class. It is compound data type, which contains any number of variables and functions. To initialize an object, you can make use of new statement to initialize object to a variable.

For Example:

  <?php
  class myclass
  {
  function getdata()
  {
  $str="Hello World!!!";
  $this->str = $str;
  }
  function display()
  {
  ?>
  <?=$this->str;?>
  <?
  }
  };
  $myvalue = new myclass;
  $myvalue->getdata();
  $myvalue->display();
  ?>
  

It will produce following output:

  Hello World!!!
  

Resources

Resources are special variable that hold references to resources external resource. Resources are created and used by special functions. The following are some of the functions, which create, use PHP resources.

  • fbsql_db_query(): It selects database and execute query.
  • ftp_connect(): It opens FTP connection to the host.
  • imap_open(): It opens IMAP stream to mailbox.
  • dba_popen(): It establishes persistent database instance for path using handler.
  • imagerotate(): it rotates images with given angle.

PHP Constants

Constant is a name or identifier for a simple value. As the name suggests, Once they are defined they cannot be changed. The value of the constants cannot be changed during runtime. Constant is like a variables used to store the information. A constant is case-sensitive by default.

The constant name starts with a letter or underscore, followed by any number of letters, numbers or underscores. To declare the constant, use define () function to retrieve the value of constant. Constant takes two parameters the first parameter takes the name of the constant and second parameter defines the value of the constant.

For example:

  <?php
  define("Name","name is:Manisha");
  echo "";
  echo (Name);    
  echo "";
  echo constant("Name");                 
  ?>
  

It will produce following output:

  name is: Manisha
  name is: Manisha
  

Difference between constants and variables

  • The main difference between constants and variables is that , constant value cannot be changed in the process of running program.
  • There is need to define $ sign before the a variable but there is a no need to define $ sign before constant.
  • Variables can be changed or redefined, but if constants once defined it cannot be redefined or changed.
  • Constants can be defined only using define() function.

PHP Operators

An operator is a something that takes one or more values or expressions and yields another value. Operators are used to perform operations on the given values. Operators perform actions on operands - they modify values of input. PHP language supports following type of operators.

  • Arithmetical Operators
  • Logical Operators
  • Comparison Operators
  • Assignment Operators
  • Conditional Operators

Arithmetical Operators

Anarithmetic operatoris a mathematical function that takes two operands and performs a calculation on them.

Operators Description
+ It adds two operands.
- It subtract second operand from first .
* It multiply both operands.
/ It divides numerator by denominator.
% It is modulus operator.

For example:

  <?php 
  $x=20; 
  $y=6;
  echo($x + $y );
  echo "";
  echo($x - $y );
  echo "";
  echo ($x * $y);  
  echo "";
  echo ($x / $y); 
  echo "";
  echo($x % $y); 
  ?>

It will produce following output:

26
14
120
3.3333333333333
2

Logical Operators

Logical operators are an instruction in which the quantity being operated on and the results of the operation can each have two values.

Operators

Description

&& (AND)

It returns true if both operands are true.

||(OR)

If any of the two operands are true then result is true or else it is false.

!(NOT)

NOT operator performs the logical negation on expression. If the expression is true then the result if false or vice versa.

Comparison Operators

Acomparison operatoris a binary operator that takes two operands whose values are being compared.

Operators

Description

==(Equal)

It checks the value of the two operands are equal or not.

!=(Is not equal to)

It checks the value of two operands are equal or not ,if values are not equal then the result is true.

>(Greater than)

It checks the value of left operand is greater than the value of right operand. If yes then the result is true.

<(Less than)

It checks the value of left operand is less than the value of right operand. If yes then the result is true.

>=(Greater than equal to)

It checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.

<=(Less than equal to)

It checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.

For example:

  <?php
      $x = 25;
      $y = 10;
      if( $x == $y ){
         echo "Result1 : x is equal to y<br/>";
      }else{
         echo "Result1 : x is not equal to y<br/>";
      }
      if( $x > $y ){
         echo "Result2 : x is greater than  y<br/>";
      }else{
         echo "Result2 : x is not greater than y<br/>";
      }
      if( $x < $y ){
         echo "Result3 : x is less than  y<br/>";
      }else{
         echo "Result3 : x is not less than y<br/>";
      }
      if( $x != $y ){
         echo "Result4 : x is not equal to y<br/>";
      }else{
         echo "Result4 : x is equal to y<br/>";
      }
      if( $x >= $y ){
         echo "Result5 : x is either greater than or equal to y<br/>";
      }else{
         echo "Result5 : x is nieghter greater than nor equal to y<br/>";
      }
      if( $x <= $y ){
         echo "Result6 : x is either less than or equal to y<br/>";
      }else{
         echo "Result6 : x is neither less than nor equal to y<br/>";
      }
  ?>
  

It will produce following output:

Result1: x is not equal to y
Result2: x is greater than y
Result3: x is not less than y
Result4: x is not equal to y
Result5: x is either greater than or equal to y
Result6: x is neither less than nor equal to y

Assignment Operators

Anassignment operatorassigns a value to its left operand based on the value of its right operand. The basic assignment operator is “equal to”.

Operators

Description

=

It assigns the values from right side operand to the left side operand

+=

It adds right operand to left operand and assign result to left operand.

-=

It subtracts right operand from left operand and assign result to left operand.

*=

It multiplies right operand with left operand and assign result to left operand.

/=

It divides left operand with right operand and assign result to left operand.

%=(

It defines modulus of two operands and assign the result to left operand.

For example:

  <?php
  $a=15; 
  echo $a;
  echo "";
   
  $b=40; 
  $b += 25;
  echo $b;
  echo "";
   
  $c=75;
  $c -= 15;
  echo $c;
  echo "";
   
  $d=9;
  $d *= 5;
  echo $d;
  echo "";
   
  $e=36;
  $e /= 9;
  echo $e;
  echo "";
   
  $f=22;
  $f %= 3;
  echo $f;
  ?>  

It will produce following result:

15
65
60
45
4
1

Conditional Operators

Conditional operatorsare used to evaluate a condition that's applied to one or twoboolean expressions. It is also called as ternary operator. It has following syntax:

  (expr1) ? (expr2) : (expr3)
  

In above syntax, expr1 is a condition when satisfied or true it evaluates expr2 otherwise expr3, for example:

  <?php
  $marks=70;
  $res=($marks>=60)?"first class":"second class";
  echo "$res";
  ?>   
  

It will display following result: first class

PHP Strings

Strings are used to represent a sequence of characters. String literals are defined with either single (‘) or double quote (“). In single quoted strings literals, escape sequences such as \n, are not recognized as anything special. In double quoted string literals, escape sequences are recognized, and embedded variables by their current values.

String Functions

The following are the functions, which are commonly used to manipulate strings in PHP.

strpos() function

The PHP strpos() function is used to search string position where the character is present. When the strpos() couldn't find the character then it returns false.

For Example:

  <?php
              echo  strpos("welcome to php!!","to");
  ?>
  

It displays following result: 8

strlen() function

The strlen( ) function is used to determine the number of character presents in the string along with space and special symbol.

For example:

  <?php
              echo  strlen ("Welcome to PHP langauge");
  ?>
   

It displays following result: 23

String Concatenation Operator

Concatenation Operator is used to join the two different strings and then represent it as one string. To concatenate two strings, use the dot (.) operator.

For example:

  <?php
              $a="Good ";
              $b="Morning";
              echo ($a. " " . $b);
  ?>
   

It will displays the following result:

Good Morning

Conclusion

In this article we saw how to set up a PHP server. We also studied PHP basics and how to execute some simple PHP programs. Hope this article helps you kick start with PHP.



I''m a full stack developer with around 10+ yrs of experience. I enjoy writing technical articles on upcoming technical trends.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login