PHP Archives - Netzole https://www.netzole.org/category/php/ Wed, 28 May 2025 01:42:19 +0000 en-US hourly 1 How to Download & Install PHP Step by Step Guide https://www.netzole.org/how-to-download-install-php-step-by-step-guide/ https://www.netzole.org/how-to-download-install-php-step-by-step-guide/#respond Tue, 28 Jan 2025 11:33:54 +0000 https://www.netzole.org/?p=13 In this tutorial, we learn how to install PHP on windows and how to install PHP with XAMPP. Install Wampserver or XAMPP on your Desktop...

The post How to Download & Install PHP Step by Step Guide appeared first on Netzole.

]]>

In this tutorial, we learn how to install PHP on windows and how to install PHP with XAMPP. Install Wampserver or XAMPP on your Desktop to quickly create your First web applications with Apache, PHP and a MySQL database.


 

  • Download latest version of PHP from http://www.php.net/downloads.php.
  • You can also download the .msi package i.e. Windows installer of PHP from sourceforge.net

For installing PHP, we will insist you to install AMP (Apache, MySQL, PHP) software stacks. It is available for all operating systems. And then we will see how to Install PHP on Windows and how to install PHP using XAMPP. There are lots of AMP options available in the market which are given below:

  • WAMP
  • LAMP
  • MAMP
  • SAMP
  • FAMP
  • XAMPP (Cross, Apache, MySQL, PHP, Perl)

Now we will see how to install PHP on windows and how to install PHP using XAMPP.


 

Setting PHP on Your Own Desktop

  • Install a web server on Your Own PC
  • Install PHP on Your Own PC
  • Install a database, such as MySQL on Your Own PC

 

Click Here to Download WAMP Server

Click Here to Download LAMP server

Click Here to Download XAMPP server

 


 

1. How To Download and Install PHP Using xampp Server Step by Step Guide on Windows

Now we will see how to install PHP using the XAMPP server in Windows.

In this PHP tutorial we will learn step by step how to download PHP and install PHP using XAMPP on different environment like Windows, Linux, Macos and server.

Download XAMPP in Window

You can download latest stable version of XAMPP from the bitnami.com official website https://bitnami.com

Once you open the website it will detect your operating system automatically and show you the correct version for your device. For example, We opened the site from 64 bit Windows systems so there is a 64 bit XAMPP package available for download. If you are from 32 bit Windows or Linux or Macos it will display the version respectively. You can also download the other versions from the download page Download | bitnami.com

Install PHP on Windows

In order to install PHP XAMPP on Windows you have to perform following actions:

Step 1 : Download the XAMPP Windows Installer Package from the bitnami.com official website as per above.

Step 2 : After downloading the XAMPP Windows Installer package, double click on the downloaded bitnami-xampp-1.0-1-windows-installer file. Now installation process has started and you will see below screen.


Step 3 :
Click on Next button, On the Next screen, Accept the terms in the license agreement and click on the Next button.

Step 4: On the Next screen, choose the location where XAMPP needs to be installed if want to install new location otherwise leave default location and then click on the Next button.

Step 5: Accept the default components as per below screen shot and click on the Next button. You don’t need to change anything in here.

Step 6: Now XAMPP is ready to install on your system. Click on the Install Button


After clicking on the Next button,XAMPP started to install at your system please keep waiting for finish installation

Step 7 : Once Installation is done. Click on the finish button. Now XAMPP is installed at your system successfully and ready to use for developing web sites.

How to verify Installation

You can verify the installation or check the version of xampp by the following steps. And run Apache and MYSQL services.

All done!

The post How to Download & Install PHP Step by Step Guide appeared first on Netzole.

]]>
https://www.netzole.org/how-to-download-install-php-step-by-step-guide/feed/ 0
PHP Session https://www.netzole.org/php-session/ https://www.netzole.org/php-session/#respond Mon, 18 Nov 2019 09:56:02 +0000 https://www.netzole.com/?p=5546 In this PHP tutorial we will see how to start Session in PHP. PHP Session A session is a process to store information (in variables)...

The post PHP Session appeared first on Netzole.

]]>

In this PHP tutorial we will see how to start Session in PHP.


PHP Session

A session is a process to store information (in variables) and further to be used this information across multiple pages. Beside in session, the information is not stored on the user’s computer, unlike a cookie.

A session creates a file in a directory on the server for a short period of time where registered session variables and their values are stored. This type of data will be available to all pages on the site during that visit.

And you can find out the location of the temporary directory file is determined by a setting in the php.ini file which is called session.save_path.

What is a PHP Session?

We understood “what is PHP Session?” through an example. Let’s suppose we are working with an application, first we open it, do some task and then close the application.

This process is similar like a Session. When we follow these processes, the computer knows who we are. And it also knows when we start the application and when we close the application.

But when we use internet, there is one issue by storing user information to be used across multiple pages (e.g. usernames, email, etc). By default, session data last until the user closes the browser.



How to Start a PHP Session

A session is simply started with the using of session_start() function.

A PHP session variables are hold in associative array which is define by $_SESSION[]. These hold information can be retrieved during lifetime of a session.

<?php
// Start the session
session_start();
?>



Let’s see an example. First we create a page “session-demo.php”. In this new page, we start a new PHP session and define some session variables:

Example#1

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Set session variables
$_SESSION["firstname "] = "abc";
$_SESSION["email"] = "test@gmail.com";
echo 'Hi, ' . $_SESSION["firstname"] . ' ' . $_SESSION["email"];?>

</body>
</html>




Destroy a PHP Session

If you want to remove all session data, use session_unset() function for the corresponding key of the $_SESSION associative array, as given in the following example:

Example#

<?php // Starting session session_start();
 // Removing session data 
if(isset($_SESSION["firstname"]))
{ unset($_SESSION["email"]); } ?>



And now to see, to destroy a session completely, simply call the session_destroy() function. When we destroy a session , the session_destroy() function does not need any argument and a single call destroys all the session data.

Example#1

<?php // Starting session 
session_start(); 
// Destroying session 
session_destroy(); ?>

The post PHP Session appeared first on Netzole.

]]>
https://www.netzole.org/php-session/feed/ 0
PHP Cookies https://www.netzole.org/php-cookies/ https://www.netzole.org/php-cookies/#respond Fri, 08 Nov 2019 11:12:21 +0000 https://www.netzole.com/?p=5526 In this tutorial, we will see how to store a minimum amount of data within the user’s browser itself using the PHP cookies. What is...

The post PHP Cookies appeared first on Netzole.

]]>

In this tutorial, we will see how to store a minimum amount of data within the user’s browser itself using the PHP cookies.


What is a Cookie?

A cookie is a small text file that lets you store a small amount of information (nearly 4KB) on the user’s computer.

Each time with the same computer the browser requests a page to the server and all the information in the cookie is automatically sent to the server within the request.

And mostly cookie is frequently used to recognize the user. With the help of PHP, you can both create and retrieve cookie values.



How to set a Cookie in PHP

Through setcookie() function, we can create or set a cookie.

Syntax:

setcookie(name, value, expire, path, domain, secure, http only);

There are mostly all parameters are optional except the parameter of the name.
There are given below, all the parameters explanation of the phpsetcookie() function:

Parameter Explanation
name The name of the cookie.
value The value of the cookie. The text file Do not store sensitive information since cookies value is stored on the user’s computer.
expires The expiry date in UNIX timestamp format. After finishing this time cookie will become inaccessible. The default value of cookie is 0 in .
path Specify the path on the server for which the cookie will be available. If set to /, the cookie will be available within the entire domain.
domain Specify the domain for which the cookie value is available to e.g www.example.com.
secure This field, if present, indicates that the cookie should be sent only if a secure HTTPS connection exists.

The post PHP Cookies appeared first on Netzole.

]]>
https://www.netzole.org/php-cookies/feed/ 0
PHP Include Files https://www.netzole.org/php-include-files/ https://www.netzole.org/php-include-files/#respond Tue, 05 Nov 2019 10:55:14 +0000 https://www.netzole.com/?p=5482 In this tutorial, we will see how to include one PHP file into another PHP file. The PHP include() and PHP require() statement allow you...

The post PHP Include Files appeared first on Netzole.

]]>

In this tutorial, we will see how to include one PHP file into another PHP file.


The PHP include() and PHP require() statement allow you to include the content of one PHP file into another PHP file. You can save your time and work using the PHP include() and PHP require() statements instead of typing the entire script multiple times.

With the help of PHP include() and PHP require() statement, we can utilize HTML code or PHP script code in many PHP script code. Let’s see the basic syntax of the PHP include() or PHP require() statement can be given listed below:

include("file path/filename"); Or include "file path/filename";
require("file path/filename"); Or require "file path/filename";


PHP include Examples

Example 1

Let’s suppose we have a standard menu php code file which is called “menu.php”:

<?php
echo '<a href="https://www.netzole.com/">Home</a> 
<a href="https://www.netzole.com/php-tutorial/">PHP Tutorial</a>
<a href="https://www.netzole.com/nodejs-tutorial/">Nodejs Tutorial
</a> 
<a href="https://www.netzole.com/python-tutorial/">Python Tutorial</a> 
?>

To include the menu file in a page, use the PHP include statement:

Example


<html>
<body>

<div class="menu">
<?php include 'menu.php';?>
</div>
</br>
<h1>Welcome to home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>

</body>
</html>

output:


Welcome to home page!

Some text.

Some more text.


Example 2

Let’s suppose we have a standard footer php code file which is called “footer.php”:

<?php
echo "<p>Copyright &copy; 2017-" . date("Y") . " Netzole.com</p>";
?>

To include the menu and footer file in a page, use the PHP include statement:

Example


<html>
<body>

<div class="menu">
<?php include 'menu.php';?>
</div>

<h1>Welcome to Netzole.com!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php';?>
</body>
</html>

output:


Welcome to Netzole.com!

Some text.

Some more text.

Copyright © 2017-2019 Netzole.com




PHP require

PHP require is used to similar to PHP include. Let’s see a PHP require example.

Example:

File: require.php


<html>
<body>

<div class="menu">
<?php require 'menu.php';?>
</div>
</br>
<h1>Welcome to home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>

</body>
</html>

output:


Welcome to home page!

Some text.

Some more text.




PHP include vs PHP require

  • require will generate a fatal error (E_COMPILE_ERROR) and stop the script
  • include will only generate a warning (E_WARNING) and the script will continue

At the top of, there is one big difference between PHP include and PHP require; when a file is included through the PHP include statement and PHP file cannot find it, the script will continue to execute: Let’s assume menu file(menu.php) does not exist.

Example


<html>
<body>

<div class="menu">
<?php include 'menu.php';?>
</div>
</br>
<h1>Welcome to Netzole.com!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php';?>
</body>
</html>

output:

Welcome to Netzole.com!

Some text.

Some more text.

Copyright © 2017-2019 Netzole.com



On the other hand,If we given the same example using the PHP require statement, the echo statement will not be execute the script because the script execution dies after the PHP require statement returned a fatal error:

Example


<html>
<body>

<div class="menu">
<h1> Home page Title!</h1>
<?php require 'menu.php';?>
</div>

<h2>Welcome to Netzole.com!</h2>
<p>Some text.</p>
<p>Some more text.</p>
<?php require 'footer.php';?>
</body>
</html>

output:

Home page Title!


The post PHP Include Files appeared first on Netzole.

]]>
https://www.netzole.org/php-include-files/feed/ 0
PHP File Upload https://www.netzole.org/php-file-upload/ https://www.netzole.org/php-file-upload/#respond Mon, 04 Nov 2019 11:51:08 +0000 https://www.netzole.com/?p=5452 In this tutorial we will see how to to upload files to the server. Steps for Uploading PHP File Upload PHP file upload features allows...

The post PHP File Upload appeared first on Netzole.

]]>

In this tutorial we will see how to to upload files to the server.



Steps for Uploading PHP File Upload

PHP file upload features allows you to upload any kind of file like images, videos, ZIP files, Microsoft Office documents, PDFs, as well as executable files and a wide range of other file types.

Step 1: Creating an HTML form

<!DOCTYPE html>
<html>
<body>

<form action="uploadfile.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="Upload_File" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

Follow these two rules for the HTML form above:
1. When we use to a file-select field the upload form must use method=”post”.
2. Always contain an enctype=”multipart/form-data” attribute.
The form which we created above sends data to a file called “uploadfile.php”, which we will create next.

Step 2: Create The Upload File PHP Script

The “uploadfile.php” file contains the following code for uploading a file:

uploadfile.php

<?php
  $folder_dir = "uploads/";
$folder_file = $target_dir . basename($_FILES["Upload_File"]["name"]);
$uploadfile = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["Upload_File"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadfile = 1;
    } else {
        echo "File is not an image.";
        $uploadfile = 0;
    }
}
?>

PHP code explained:

  • $folder_dir = “uploads/” – specifies the directory where the file is going to be placed
  • $folder_file specifies the path of the file to be uploaded
  • $uploadfile=1 is not used yet (will be used later)
  • $imageFileType holds the file extension of the file (in lower case)
  • Next, check if the image file is an actual image or a fake image



Use move_uploaded_file() function for uploading files

This function used to moves the uploaded file to a new location. The move_uploaded_file() function checks first internally if the file is uploaded thorough the POST request. It moves the file if it is uploaded through the POST request.

<?php  
$folder_dir = "uploads/";
$folder_file = $target_path.basename( $_FILES['Upload_File']['name']);   
  
if(move_uploaded_file($_FILES['Upload_File']['tmp_name'], $target_path)) {  
    echo "File uploaded successfully!";  
} else{  
    echo "Sorry, file not uploaded, please try again!";  
}  
?> 

The post PHP File Upload appeared first on Netzole.

]]>
https://www.netzole.org/php-file-upload/feed/ 0
PHP Date and Time https://www.netzole.org/php-date-and-time/ https://www.netzole.org/php-date-and-time/#respond Mon, 04 Nov 2019 06:40:43 +0000 https://www.netzole.com/?p=5424 In this PHP tutorial we will see how to format the date and time. The PHP Date() Function The PHP Date() function is used to...

The post PHP Date and Time appeared first on Netzole.

]]>

In this PHP tutorial we will see how to format the date and time.



The PHP Date() Function

The PHP Date() function is used to formats a timestamp to a more readable date and time.
The computer stores dates and times in a format called UNIX Timestamp, which measures time as a number of seconds since the beginning of the Unix epoch (midnight Greenwich Mean Time on January 1, 1970 i.e. January 1, 1970 00:00:00 GMT ). For Example….See below:

Example#1

<?php
   print time();
?>

Output:

1572854758

Since this is not readable(1572854758) format for humans being to read, For humans convenient , PHP converts a timestamp to a format that is readable to humans and dates from your notation into a timestamp the computer understands. The syntax for the PHP date() function can be given below.

Syntax:


   date(format,timestamp)

There are two important facts generated which given below:
1. The format parameter in the date() function is required which is tell us to specifies the format of returned date and time.
2.Second think we can say timestamp is an optional parameter, if not included then current date and time will be used as a output. The following example clearer your all query which is displays today’s date:

Example#2

<?php  
  
$today = date("d/m/Y");
echo $today;
  
?> 


Types of Parameters the Dates with PHP

There are some the date-related formatting characters that are commonly used in format string which given below and also see a example for batter understanding :

  • d – Represent day of the month; (01 or 31)
  • D – Represent day of the week in text as an abbreviation (Mon to Sun)
  • m – Represent month in numbers with leading zeros (01 or 12)
  • M – Represent month in text, abbreviated (Jan to Dec)
  • y – Represent year in two digits (08 or 14)
  • Y – Represent year in four digits (2008 or 2014)

The section of the date can be separated by putting other characters, like hyphens (-), dots (.), slashes (/), or spaces to add additional visual formatting.

Example#3


<?php
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
echo "Today is " . date("l");
?>


Types of Parameters the Times with PHP

Similarly as a Date Formatting you can use the following characters to format the time string see below:

  • h – Represent hour in 12-hour format with leading zeros (01 to 12)
  • H – Represent hour in in 24-hour format with leading zeros (00 to 23)
  • i – Represent minutes with leading zeros (00 to 59)
  • s – Represent seconds with leading zeros (00 to 59)
  • a – Represent lowercase ante meridiem and post meridiem (am or pm)
  • A – Represent uppercase Ante meridiem and Post meridiem (AM or PM)

Example#3


<?php
echo date("h:i:s") . "<br>";
echo date("F d, Y h:i:s A") . "<br>";
echo date("h:i a");
?>




The PHP time() Function

The PHP time() function is used to get the current time as a Unix timestamp. For example:

Example#

<?php
   print time();
?>

Output:

1572854758

For humans convenient this output ,PHP converts a timestamp to a format that is readable to humans introduce date() function.

Example#

<?php
 $timestamp = 1572854758;
echo(date("F d, Y h:i:s", $timestamp));
?>

Output:

November 04, 2019 08:05:58




PHP Date & Time Function with More Example

PHP Date/Time Common Functions
Functions What it Does
checkdate() Checks a date for Validity
strtotime() Creates Timestamps from English-language Descriptions
gmdate() Expresses a timestamp in GMT
date_diff() It will Returns Difference between two dates
date( ) Formats a local date and time
getdate() Returns date/time information of a timestamp or the current local date/time
mktime() Returns the Unix timestamp for a date
time() Returns the current time as a Unix timestamp

The post PHP Date and Time appeared first on Netzole.

]]>
https://www.netzole.org/php-date-and-time/feed/ 0
PHP Loops https://www.netzole.org/php-loops/ https://www.netzole.org/php-loops/#respond Fri, 18 Oct 2019 05:08:29 +0000 https://www.netzole.com/?p=5379 PHP Loops Loops are used to execute a block of statements, multiple times until and unless a specific condition is fulfilled. The basic idea behind...

The post PHP Loops appeared first on Netzole.

]]>
PHP Loops

Loops are used to execute a block of statements, multiple times until and unless a specific condition is fulfilled. The basic idea behind a loop to helps the user to save both time and effort of repetitive tasks multiple times.
PHP supports four different types of loops.

  • for loop
  • foreach loop
  • while loop
  • do-while loop



1. The PHP for Loop

The for loop is used when user knows in advance how many times the block of code should run.

Syntax:

for (initialization expression; test condition; update expression) {
// code to be executed
}

In PHP for loop, a loop variable is used to control the loop. First, initialize this loop variable to some value, then check whether this variable is less than or greater than the counter value.

If statement is true, then loop body is executed and loop variable gets updated . Steps are repeated till exit condition comes.

  • Initialization Expression: In this expression we have to initialize the loop counter to some value. for example: $num = 0;
  • Test Expression: In this expression we have to test the condition. If the condition evaluates to true then we will execute the body of loop and go to update expression otherwise we will exit from the for loop. For example: $num <= 7;
  • Update Expression: After executing loop body this expression increments/decrements the loop variable by some value. for example: $num += 1;

Example#1

<?php  
  
// code to illustrate for loop 
for ($x = 0; $x <= 7; $x++) { 
    echo "The number is: $x <br>"; 
}  
  
?> 

Output:

The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7


Example#2

<?php  
  
// code to illustrate for loop 
for ($x = 1; ; $x++) {
    if ($x > 7) {
        break;
    }
   echo "The number is: $x <br>"; 
}
  
?> 


Example#3

<?php  
  
$x = 1;
for (; ; ) {
    if ($x > 10) {
        break;
    }
   echo "The number is: $x <br>"; 
    $x++;
}
  
?> 




2. PHP foreach Loop

The PHP foreach Loop construct provides an easy way to iterate over arrays. foreach works only on arrays and objects and will create an error when you try to use it on a variable with a different data type.

Syntax:

foreach($array as $value){
// Code to be executed
}


Example#1

<?php
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {
    echo "$value <br>";
}
?>

Output:

red
green
blue
yellow




The PHP while Loop

The while loop is very simple loop in PHP. The while loop executes a block of code as long as the specified condition is true.

Syntax:

while (condition is true) {
code to be executed;
}


Example

<?php
$x = 1;

while($x <= 4) {
    echo "The number is: $x <br>";
    $x++;
}
?>

Output:

The number is: 1
The number is: 2
The number is: 3
The number is: 4


The post PHP Loops appeared first on Netzole.

]]>
https://www.netzole.org/php-loops/feed/ 0
PHP Switch Case Statements https://www.netzole.org/php-switchcase-statements/ https://www.netzole.org/php-switchcase-statements/#respond Thu, 17 Oct 2019 11:19:10 +0000 https://www.netzole.com/?p=5370 In this tutorial, we will learn about How to use PHP Switch Case Statements. PHP Switch Caseis the alternatives of PHP else if statement and...

The post PHP Switch Case Statements appeared first on Netzole.

]]>
php switch case
In this tutorial, we will learn about How to use PHP Switch Case Statements. PHP Switch Caseis the alternatives of PHP else if statement and can be used in place of PHP else/if.

The switch case php is used to execute one of many blocks of code. The switch statement is similar to a series of IF statements on the same condition.

PHP Switch Case Statements

Syntax:

switch (n) {
case n1:
code to be executed if n=n1;
break;
case n2:
code to be executed if n=n2;
break;
case n3:
code to be executed if n=n3;
break;

default:
code to be executed if n is different from all n1…n3;
}

 


PHP switch case example

switch ($i) {
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
}

 

Output:

 

i equals 0

 


 

Example #2 php switch case allows usage of strings

<?php switch ($i) { case "apple": echo "i is apple"; break; case "bar": echo "i is bar"; break; case "cake": echo "i is cake"; break; } ?>

The post PHP Switch Case Statements appeared first on Netzole.

]]>
https://www.netzole.org/php-switchcase-statements/feed/ 0
PHP If…Else Statements https://www.netzole.org/php-ifelse-statements/ https://www.netzole.org/php-ifelse-statements/#respond Thu, 17 Oct 2019 07:41:02 +0000 https://www.netzole.com/?p=5338 PHP If else Condition In this PHP tutorial, today we learn PHP If else Condition with the help of examples. PHP Conditional statements are used...

The post PHP If…Else Statements appeared first on Netzole.

]]>
PHP If else Condition

In this PHP tutorial, today we learn PHP If else Condition with the help of examples.

PHP Conditional statements are used to perform different actions based on the results of a logical or comparative test conditions at run time

PHP Conditional Statements

There are many statements in PHP that you can use to perform different actions which is given below:

  • The if statement
  • The if…else statement
  • The if…elseif….else statement
  • The switch…case statement

Now Let’s start to explain each of these conditions statements see below:

 


 

PHP – The if Statement

The if statement is used to execute some code only if the condition is true.

Syntax:

 

if (condition) {
//code to be executed here
}

Flowchart:

Example

<?php  
$num=50;  
if($num<60){  
echo "$num is less than 60";  
}  
?>  

 

Output:

 

50 is less than 100

 


 

PHP – The if…else Statement

The if…else statement executes some code if a condition is true and another block of code if it is false.

Syntax:

 

if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}

 

Example

<?php  
$num=12;  
if($num%2==0){  
echo "$num is even number";  
}else{  
echo "$num is odd number";  
}  
?>  

 

Output:

 

12 is even number

 


 

PHP – The if…elseif…else Statement

The if…elseif…else a special statement that is used to more than two conditions.

Syntax:

 

if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if first condition is false and this condition is true;
} else {
code to be executed if all conditions are false;
}

 

Example

<?php  
$d = date("D");
if($d == "Sat"){
    echo "Have a nice weekend!";
} elseif($d == "Sun"){
    echo "Have a nice Sunday!";
} else{
    echo "Have a nice day!";
}
?>  

 

Output:

 

Have a nice day!

Will will explain about PHP switch-case statement in the next chapter.

The post PHP If…Else Statements appeared first on Netzole.

]]>
https://www.netzole.org/php-ifelse-statements/feed/ 0
PHP Arrays https://www.netzole.org/php-arrays/ https://www.netzole.org/php-arrays/#respond Tue, 01 Oct 2019 08:37:44 +0000 https://www.netzole.com/?p=5297 PHP Arrays An array is a data structure that holds multiple values in one single variable at a time. Let’s see you have a multiple...

The post PHP Arrays appeared first on Netzole.

]]>
PHP Arrays

An array is a data structure that holds multiple values in one single variable at a time.

Let’s see you have a multiple items (a list of fruit names, for example), storing the fruit in single variables could look like this:

$fruit1 = “apple”;
$fruit2 = “kiwi”;
$fruit3 = “papaya”;

Now if you want to loop through the fruits and find a specific one? And what if you had lots of fruit ?
The solution is to create an array for this problem!. Let’s starts with how to create an array?.
In PHP, the array() function is used to create an array:

array();




Types Of Array

There are three types of array in PHP which is given below and describing array one by one:

  • Indexed array — An array with a numeric element.
  • Associative array — An array where each element has its own specific value.
  • Multidimensional array — An array containing one or more arrays within itself.




Indexed array

An indexed or numeric array holds each array value with a numeric index. There are two ways of creating an indexed array.
a. Simple Way: The index can be assigned automatically (index always starts at 0) .For an example see given below:

Example

<?php
// Define an indexed array
$fruits= array("Apple", "Orange", "Papaya");
?>

Output:

Array ( [0] => Apple [1] => Orange [2] => Papaya)



b. Indexes are Assigned Manually Way:

Example

<?php
$fruits[0] = "Apple"; 
$fruits[1] = "Orange"; 
$fruits[2] = "Papaya"; 
?>

Output:

Array ( [0] => Apple [1] => Orange [2] => Papaya)




Associative array

The post PHP Arrays appeared first on Netzole.

]]>
https://www.netzole.org/php-arrays/feed/ 0