PHP 7 Tutorial: Introduction for Beginners (Basic)

php 7 tutorial

PHP 7 Tutorial: Introduction for Beginners (Basic)

PHP: Hypertext Preprocessor is a general purpose server side script language. it designed for web development. It designed by Rasmus Lerdorf in 1994 and first released in 1995. PHP is free to download and use. It can contain text, HTML, JavaScript, CSS and PHP code. it runs on Windows, Linux, Mac OS and etc… PHP can do many things such as generate dynamic web pages, manage data in your database, control user access, etc… PHP 7 is much faster than the previous versions. In this tutorial. You will learn to install and run PHP on Windows pc.

Official Page : https://www.php.net/

Check this – Top 20 Programming Languages

Basic PHP Structure.

<?php
echo "Hello World!";
?>

PHP 7 with HTML

<!DOCTYPE html>
<html>
<body>

<?php
echo "Hello World!";
?>

</body>
</html>

Output: Hello World!

PHP 7 – Installation (Using XAMPP)

XAMPP – Apache (A) + MariaDB(M) + PHP(P) + Perl(P)

CtechF - How to install XAMPP - Screenshot

XAMPP is an Open source software. It’s a Stands of Cross-platform (X). we can create a local web server using XAMPP. It’s easy to use and it works with Windows, Linux, and Mac OS. You can use alternative software for XAMPP, such as WAMP, MAMP.

XAMPP Download link (121 MB) – https://www.apachefriends.org/index.html
WAMP download link (286 MB) – http://www.wampserver.com/en/#download-wrapper
MAMP download link (410 MB) – https://www.mamp.info/en/downloads/

How to install XAMPP.

  • Click on the downloaded XAMPP setup. (If you got any permission message or warning message click “OK” or “YES” button to continue) It will open the XAMPP setup wizard. Click the next button to continue.
CtechF - How to install XAMPP - Screenshot

Now it asked, what components we want to install and what’s not. We only need Apache and MySQL. (select following Components and click the next button to continue)

  • Server
    • Apache – Apache Server
    • MySQL – Database Server
  • Program Language
    • PHP – Hypertext Preprocessor
  • Program Languages
    • phpMyAdmin – Database Control panel
CtechF - How to install XAMPP - Screenshot
  • Choose installation location and click Next (Default: C:\xampp)  (If you got Windows Defender Firewall Permission request, Tick both checkboxes and click Allow access.)
  • Click the next button to continue Now it will install XAMPP (wait till the installation complete) After the installation complete. Click Finish.
  • It will open the XAMPP Control panel automatically.
  • This is a XAMPP Control Panel.
CtechF - How to install XAMPP - Screenshot
  • Now click Start Apache and MySQL (It will start Apache and MySQL servers)
CtechF - How to install XAMPP - Screenshot
  • Create some .php files and place them in your web directory (htdocs folder. – C:\xampp\htdocs). Now open your web browser and type http://localhost  or http://127.0.0.1 on the address bar. then navigate your .php file.

Example:
Create info.php file and enter the following script. Then place it in your web directory.

<?php
phpinfo();
?>

Now open your web browser and type http://localhost/info.php  or http://127.0.0.1/info.php on the address bar.

more details: https://php.net/manual/en/install.php

PHP 7 – Tutorial

PHP 7 – Comments 

<?php
// Single line comment
# Single line comment

/*
multiple-lines 
comment
hello world!
*/
?>

PHP 7 – Display Text

<?php
echo "Hello World!";

print "<h1><i>PHP</i></h1>";
?>

Output:

PHP 7 – Variables

<?php
$domain = "ctechf.com";
$a = 20;
$b = 152.54;
$text = "I'm $a years old";

echo $a;
echo $domain;
echo $text;
?>

Output:

PHP 7 – Strings

<?php
$domain = "ctechf.com";

echo strlen($domain); //String length
echo "<br/>";
echo strrev($domain); // Reverse string
echo "<br/>";
echo str_word_count($domain); // Number of words
echo "<br/>";
echo strpos($domain,"h"); //Find Posision

?>

Output:

* escape-sequence replacements

  • \n is replaced by the new line.
  • \$ is replaced by the dollar sign ($)
  • \\ is replaced by a single backslash (\)
  • \” is replaced by a single double-quote (“)
  • . use to concatenate two string variables together (String Concatenation Operator)
<?php
$a = 20;
$text1 = "I'm $a years old";
$text2 = "I'm \$a years old";
echo $text1 . "<br/>";
echo $text2 . "<br/>";


$s1="Hello";
$s2="World";
echo $s1 . " " . $s2; //String Concatenation Operator
?>

Output:

PHP 7 – Arrays

* Numeric Array – A array with a numeric index.

<?php
$numbers1 = array( 8, 4, 5, 7, 22); //Method 1

echo $numbers1[0];
echo $numbers1[1];
echo $numbers1[2];
echo $numbers1[3];
echo $numbers1[4];

echo "<br/>";
//Method 2
$numbers2[0] = 8;
$numbers2[1] = 4;
$numbers2[2] = 5;
$numbers2[3] = 7;
$numbers2[4] = 22;

echo $numbers2[0];
echo $numbers2[1];
echo $numbers2[2];
echo $numbers2[3];
echo $numbers2[4];

//You can use foreach for display array values
?>

Output:

* Length of an Array

<?php
$numbers = array( 8, 4, 5, 7, 22);
echo count($numbers);
?>

Output: 5

* Associative Array – A array with a strings as index.

<?php
$marks1 = array("John" => 95, "Octavia" => 75, "Tro" => 50, "Nixon" => 81); //Method 1

echo "<b>John Marks: </b>" . $marks1['John'] . "<br>";
echo "<b>Octavia Marks: </b>" . $marks1['Octavia'] . "<br>";
echo "<b>Tro Marks: </b>" . $marks1['Tro'] . "<br>";
echo "<b>Nixon Marks: </b>" . $marks1['Nixon'] . "<br>";

//Method 2
$marks2['John'] = 95;
$marks2['Octavia'] = 75;
$marks2['Tro'] = 50;
$marks2['Nixon'] = 81;

echo "<br> Method 2 </br>";
echo "<b>John Marks: </b>" . $marks2['John'] . "<br>";
echo "<b>Octavia Marks: </b>" . $marks2['Octavia'] . "<br>";
echo "<b>Tro Marks: </b>" . $marks2['Tro'] . "<br>";
echo "<b>Nixon Marks: </b>" . $marks2['Nixon'] . "<br>";

?>

Output:

* Multidimensional Array – A array contain one or more arrays 

<?php
$details = array( 
    "newt" => array (
         "fname" => "Newton",
         "gender" => "Male",	
         "year" => 1897,
         "maritals" => "Married"
    ),
    "sam" => array (
         "fname" => "Sam",
         "gender" => "Male",	
         "year" => 1983,
         "maritals" => "Single"
    ),
    "elio" => array (
         "fname" => "elio",
         "gender" => "Male",	
         "year" => 1965,
         "maritals" => "Single"
    ),
    "nora" => array (
         "fname" => "Nora",
         "gender" => "Female",	
         "year" => 2003,
         "maritals" => "Single"
    ),
);

echo $details['nora']['year'] . "<br />"; 

foreach ($details as $names) {
  echo "<b>Name: </b>" . $names['fname'] . "<br/>";
  echo "<b>Gender: </b>" . $names['gender'] . "<br/>";
  echo "<b>Birth Year: </b>" . $names['year'] . "<br/>";
  echo "<b>Maritals Status: </b>" . $names['maritals'] . "<br/>";

  echo "<br/>";
}
?>

Output:

Operators

* Arithmetic Operators

* Comparison Operators

* Logical Operators

* Increment / Decrement

<?php
$no = 150;

echo ++$no . "<br/>"; //Pre increment ($no = $no + 1 then print $no)
echo --$no . "<br/>"; //Pre decrement ($no = $no - 1 then print $no)
echo $no++ . "<br/>"; //Post increment (print $no then $no = $no + 1)
echo $no-- . "<br/>"; //Post decrement (print $no then $no = $no - 1)

?>

Output:
151
150
150
151

PHP 7 – Conditional Statements

Read: How to be a Programmer | Part 4 : Control Structures

* if Statement

if (condition) {
    //Code
}
<?php
$no1 = 100;
$no2 = 100;

if ($no1 == $no2) {
    echo "No1 = No2";
}
?>

Output: No1 = No2

* if-else Statement

if (condition) {
    //code
} else {
    //code
}
<?php
$no1 = 100;
$no2 = 80; 

if ($no1 > $no2) {
    echo "No1 is Greater than No2";
} else {
    echo "No2 is Greater than No1";
}
?>

Output: No1 is Greater than No2

* if-elseif-else Statement

if (condition) {
    //code
} elseif {
    //code
} else {
    //code
}
<?php
$marks = 73;

if ($marks >= 75) {
    echo "A";
} elseif ($marks >= 65)  {
    echo "B";
} elseif ($marks >= 55)  {
    echo "C";
} elseif ($marks >= 35)  {
    echo "S";
} else {
    echo "F";
}
?>

Output: B

* switch Statement

This statement used to execute different codes based on different conditions.

switch (input) {
    case "condition":
        //Code
        break;
    case "condition":
        //Code
        break;
    case "condition":
        //Code
        break;
    case "condition":
        //Code
        break;
    ...
    ...
    ...
    ...
    ...
    default:
        //Code
}
<?php
$month = "5";
$monthlabel = "";

switch ($month) {
    case "1":
        $monthlabel = "January";
        break;
    case "2":
        $monthlabel = "February";
        break;
    case "3":
        $monthlabel = "March";
        break;
    case "4":
        $monthlabel = "April";
        break;
    case "5":
        $monthlabel = "May";
        break;
    case "6":
        $monthlabel = "June";
        break;
    case "7":
        $monthlabel = "July";
        break;
    case "8":
        $monthlabel = "August";
        break;
    case "9":
        $monthlabel = "September";
        break;
    case "10":
        $monthlabel = "October";
        break;
    case "11":
        $monthlabel = "November";
        break;
    case "12":
        $monthlabel = "December";
        break;
    default:
        $monthlabel = "Not valid";
}

echo $monthlabel;
?>

Output: May

PHP 7 – Loops

* while Loop

while (condition) {
    //Code
}
<?php
$a = 0;

while ($a < 5) {
    echo "Hello <br/>";
    $a++;
}
?>

Output:

* do-while Loop

do-while loop will always execute the code once before the check condition

do {
    //Code
} while (condition);
<?php
$a = 0;

do {
    echo "Hello <br/>";
    $a++;
} while ($a < 5);
?>

Output:

* foreach Loop

foreach loop works only on arrays.

foreach ($array as $value) {
    //code
}
<?php 
$names = array("john", "elio", "troye", "green"); 

foreach ($names as $val) {
    echo "$val <br>";
}
?>

Output:

* for Loop

for (initialization; condition; increment) {
    //code
}
<?php 
for ($a = 0; $a < 5; $a++) {
    echo "Hello! <br>";
}
?>

Output:

PHP 7 – Functions

* Function

function functionName() {
    //codes
}
<?php
function helloworld() {
  echo "Hello World!";
  echo "<br>";
}

helloworld();
helloworld();
?>

Output:

Hello World!
Hello World!

* Function Arguments

function functionName($variable, $variable) {
    //codes
}
<?php
function plus($no1, $no2) {
    echo "Total: " . ($no1 + $no2) . "<br/>";
}

plus(12, 25);
plus(100, 985);
plus(25.2, 25);
?>

Output:

* Default Argument Value

function functionName($variable = value, $variable = value) {
    //codes
}
<?php
function plus($no1 = 100, $no2 = 200) {
    echo "Total: " . ($no1 + $no2) . "<br/>";
}

plus(12, 25);
plus(100, 985);
plus(25.2, 25);
plus(); //Default $no1 = 100, $no2 = 200 
?>

Output:

* Returning values

return statement use to let a function return a values.

function functionName($variable, $variable) {
    //codes
    return value;
}
<?php
function plus($no1 = 100, $no2 = 200) {
    $sum = $no1 + $no2;

    return $sum;
}

echo "Total: " . plus(12, 25) . "<br/>";
echo "Total: " . plus(100, 985) . "<br/>";
echo "Total: " . plus(25.2, 25) . "<br/>";
echo "Total: " . plus(); //Default $no1 = 100, $no2 = 200 
?>

Output:

Sources: 
https://pixabay.com (Images)
https://www.php.net/manual/en/ (Details)

Hi Friends,

Hope this post (PHP 7 Tutorial) will help you to learn something, If you enjoy my work then please Share my posts with your buddies and anyone who might be interested in programming and don’t forget to Comment and Subscribe.

What do you think about this post?
See you in the comment section.

Like ctechf Facebook fan page : https://www.facebook.com/ctechf

Also Read :

Liked it? Take a second to support CtechF on Patreon!

Leave a Reply

140 Shares
Share
Tweet
Share
Pin
Share