PHP Programming Examples for Beginners

What is PHP?

Php is “Hypertext Preprocessor”. Php is General-Purpose Scripting Language widely used for Server-Side Script Language is an open Source. Php script language can be interpreted on the server. Php Programming language and it’s embedded into HTML(Hypertext markup language). Php helpfully for application & web development (static, dynamic, e-commerce website designing).Created in 1994 by RASMUS LERDOGF.

What do you mean by server side language?

Php is a server-side language. That’s mean we are writing any program and program goes to the server and then execute the result displayed on the browser pages. 

What are the main characteristics of php?

Php are some characteristics as like –

  • Provide more efficiency.
  • Provide Flexibility & Security.
  • Provide simplicity & Familiarity.

How can save our Php code/file & Extension name.

We are write down a PHP code/ programming and we want to save this file then you can press Ctrl + S and then select specific space for saving your code/program with your specific folder with ‘.php’ extension. This is very good Php programming examples for beginners.

For example:- index.php

In this example index your page name and ‘.php’ extension name. Is tutorial help for PHP programming examples for beginners.

What is Common uses of Php:-

  • Through php, you can modify, create, update, delete data in the database.
  •   With the help of PHP, we are creating a form. Send & retrieve information from the database in the server and when you need a data you can access the information and do some more updates in the data. Access cookies variable. 

Needs for installation in php:-

The Php community provided some required software like as:-

First you have required php software installation download this link http://php.net/  and xampp server download this link https://www.apachefriends.org/download.html  and install php and xampp server in the system.

What is the Benefits of php?

  •   First and most benefit of Php is free software you can’t be paid any charges to purchase php software. Download PHP software on php site free  http://php.net/
  • The second benefit of PHP is support/compatible with almost all server like as Apache Server, WAMP Server, LAMP Server, XAMPP Server, IIS, MySQL Server.
  • The PHP is compatible and runs on all various platforms like as- Windows, Mac, Unix etc.
  • Php is very easy to learn and run programs in the server.

Php examples with Source code and output

How can write down the code in PHP?  And what is Basic syntax?

You have to know that starting and Ending syntax in the php.

                This is most commonly used syntax.

<?php
Echo “Hey! This is the basic syntax of php.”;
?>

In this example you can see two tags first starting/begin tag <?php and end tag ?> and echo is printing statement to displayed your string content in the double quotes. And semicolon terminates script as run time.

For Example: –     php programming examples for beginners

<!DOCTYPE html>

             <html>

             <head>

             <title>star academy</title>

             </head>

             <body>

             <h1>This is my first website.</h1>

            <?php

            echoHello guy’s I m star Academy”;

            ?>

           </body>

             </html>

In this example you learn about some tags title tag, head tags, title tag, heading tag, body tag and HTML 5 tag <!DOCTYPE html> all tags are called HTML tag .In this example echo are print your statement (“Hello guy’s I m star academy”;) in the browser.

 Output our example after running this code:-

This is my first website
  Hello guy’s I m star Academy

php programming examples for beginners :-Learn using paragraph & heading tag in the syntax.

<!DOCTYPE html>

             <html>

             <head>

             <title>Star Academy</title>

             </head>

             <body>

             <h1>This is my first website.</h1>

            <p>Hello guy’s I m star Academy</p>

           </body>

             </html>

  • In these example you can learn about paragraph tag <p>, heading tag <h1> in the syntax. You can use heading tag someone makes a bold. And paragraph tag use writes about some things text/content in your website.

Output :-
This is my first website
Hello guy’s I m star Academy

What is variable in php?

A variable is used to store and manipulate data or values in programs. A variable is a name that refers to a memory location where some data value is stored. You can assign different values to a variable during program execution.

In this programming language, you can needs variables to store information or value like as text-string, number or array. If you have created a program and you need store information or value, text string then you need for the variable.

For Example:-   

<?php

$value_variable = 20;

$value_variable = “This is the example of variable”;

?>

In this tutorial, you learn about declaring a variable and store the value in the variables. In this example, you have created two variables like as $value_variable, $value_variable2, and both variables store are different-different values.

In the PHP programming, all variables begin with the $ sign. Don’t forget using semicolon end of syntax. Because if you forget using the semicolon end of the syntax than you will get an unexpected error in the programming.

What are the benefit of variable in the php:-

The most benefit of the variable in the PHP In the PHP programming you have to create a variable and you don’t have to determine the data type of the variable.

For Example:-  

int new_var  = 20;

string new_var2 = “hello world”;

This two example use for C & C++ programming with determines data type. But Php automatically convert the variable with the correct data type without determining data type.

Rules of variables creating:-

  • A variable name cannot contain spaces use underscore (_) in the space.
  • A variable name must have started with a letter or underscore ( _ ).
  • A variable name only alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ ).

Needs for data type:-

The data stored in a memory of the computer can be of many types. For example– a person’s age is stored as a numeric value and an address is stored as an alphanumeric character. Data types are used to define the operation possible on variables and the storage method.

Types of Data type:-

String
Integer
Float
Null

String: – The String is a data type and is used for programming when a programmer needs to assign to character type value like “Hello world”. And string value is written only inside in the double quotes.

For Example:-

<?php

$v = “Hey! This example denote string.”;

echo $v;

?>

Integer: – Integer is a data type and integer is denoted int sign. Integer use for programming when a programmer needs to assign to integer type value like 1 2 3 678 785…so on or is a non-decimal number, an integer value doesn’t write any quotes.

<?php

              $v = 34;

             Var_dump ($v);

?>

Float: – float(floating point number) is a data type with a decimal point. Can store fractional numbers.  Float use for programming when a programmer needs assign to float type value like 1.2, 3.6.78 78.5…

<?php

          $v = 34.5;

            var_dump($V)

?>

Boolean: – Can represent/store two possible statement like as: true or false. Is depended upon condition. The Boolean data type is required when a condition has to be checked. The true or false value of the expression or the condition determines further execution of the PHP program.

Array: – An array is important for the programmer because of an array store multiple values in the one single variable.  Array mostly used in programming languages..

<?php
$bikes = array (“Honda”, “hero”, “splender”);
Var_dump ($bikes);
?>

Needs for operator:- In the Arithmetical, Logical, Assignment, Comparison and concatenation (string) PHP programming we will look at php operator is very helpfully for logical programming solving.

•Arithmetical Operator
•Logical Operator
•Comparison and concatenation (string) operator

operator Examples
+=   x+=y  
-= x-=y
= x=y
*= x*=y
/= x/=y
.= x.=y
%= x%=y
  1.  Arithmetic Operator:-
Addition Subtraction Division Multiplication Increment Decrement Modulus (division remainder)
+ / * ++ %
x=5
x+2
x=6
10-x
20/5 x=4
x*5
x=4
x*5
x=5
x–
10%8

2. Logical Operator:-

And Or Not
&& || !
x=5
y=5
(x < 10 && y > 1) returns true
x=5
y=5
(x==5 || y==5) returns true
x=5
y=5
!(x==y) returns false

3. Comparison operator:-

is equal to is not equal is not equal is less than is greater than is less than or equal to is greater than or equal to
== !== <>  <= >=
1==2 returns false 1!=2 returns true 1<>2 returns true 1<2 returns true 1>2 returns false 1<=2 returns true 1>=2 returns false

Needs for Switch case:-

In the PHP programming programmer needs for switch condition because of the switch condition use which place where ever one statement play more condition and more action. That’s mean the one statement perform the different action based on different condition.

For Example:-

<?php

                   Switch (day)

                   {

                                  Case 1:

                                   echo “Today Suday”;

                                          break;

                                                   Case 2:

                                                   echo “Toady Monday”;

                                                        break;

                                  Case 3:

                                  echo “Today Tuesday”;

                                      break;

                                                   Case 4:

                                                   echo “Toady Wednesday”;

                                                         break;

                                Case 5:

                                 echo “Toady Thursday”;

                                   break;

                                              Case 6:

                                              echo “Today Friday”;

                                                  break;

                              Case 7:

                              echo “Today Saturday”;

                                 break;

           default:

                    echo  “Do not match any number.”;

              }

            ?>

Needs if else Conditional Statements: –   

In this tutorial, you learn about if else condition because if you have a different action for a different condition than it is very important to the programming loop to perform one condition run.

If condition executes when some code if one condition is true and all condition is false than get the else condition and display error.

<?php

if ($population > $age):

 echo  $population. “ is greater than “ .$age;

else if  ($population  = =  $age):

echo  $population. “  equal  “ .$age;

else:

echo $population." is neither greater than or equal to ".$age;
end if;

?>

In this syntax, we have two conditions available, but in this situation only one condition execute, loop get first if condition, when the statement is true than program, executes the first statement and display the result, else condition false than loop came to other if statement cheek the condition and again repeat this process. Else any statements are false than program get displayed else part.

What is loop?

Looping statements are used when you want a section of a program to be repeated a certain number of times.

Difference between While or do while loop:-

While:- While loop condition expression. The while loop executes a statement or a block of statements as long as the evaluating condition remains true. The evaluation and must return a Boolean value that can be true or false.

The while loop checks for a condition before executing the body of the loop.

The Figure is blow to understanding the while loop:-

For Example:-

                 <?php
                  $num = 1;
                   While  ($num  <=  10);
                      {                          
   echo  “The number is :- $num <br>”;                              $num++;                   
    }            
  ?>

Do- while loop: – In the while loop, the condition is evaluated at the beginning of the loop. If the condition is false, the body of the loop is not executed. If the body of the loop needs to be executed at least once, then the do-while loop should be used. The do –while loop placed the test condition at the end of the loop. The do keyword marks the beginning of the loop from the body of the loop. Finally, the while statement provides the condition that checks whether the loop will execute again or not ends the body of the loop.

In the do-while loop, first the body of the loop is executed and then the conditional expression is evaluated.

The do-while loop main difference is when the condition is true then the statement executed again-and-again, if the condition is false then the do-while loop execute you statement only one time. That is the main difference do-while loop.  

The Figure is blow to understanding the do-while loop:-

For Example:-

                 <?php                  
  $num = 1;                   
Do                      
  {                          
  echo  “The number is :- $num <br>”;
$num++;                      
  }                   
  While  ($num  <=  10);           
  ?>

Needs for Loop in the php:-

In the for loop, control goes to the iteration expression first and then to the conditional expression. The for loop checks the validity of a condition first and then executes the body of the loop. For loop execute the statement at a specified number of time.

<?php

for ($x = 0;  $x <= 5;  $x++)

{

echo  “The number is:- $x  <br>  ”;

}

?>

In this code you learn about for loop condition. In this loop given a starting point ($x = 0) and then specific number of time ($x <= 5) and then increment of the numbers ($x ++).

You may also like...

Popular Posts

Leave a Reply

Your email address will not be published. Required fields are marked *