WWW.APPSERVGRID.COM

Phorum about AppServers & Grid Technologies
It is currently Sun Nov 03, 2024 10:56 pm

All times are UTC + 2 hours [ DST ]




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Mon Feb 11, 2019 3:00 am 
Offline
Site Admin

Joined: Tue Jan 25, 2011 8:51 pm
Posts: 49
Welcome to PHP

PHP: Hypertext Preprocessor (PHP) is a free, highly popular, open source scripting
language. PHP scripts are executed on the server.

Just a short list of what PHP is capable of:
- Generating dynamic page content
- Creating, opening, reading, writing, deleting, and closing files on the server
- Collecting form data
- Adding, deleting, and modifying information stored in your database
- controlling user-access
- encrypting data
- and much more!

Before starting this tutorial, you should have a basic understanding of HTML.
PHP has enough power to work at the core of WordPress, the busiest blogging system on the web.
It also has the degree of depth required to run Facebook, the web's largest social network!
--------------------------------------------------------------------------------------------
Why PHP

PHP runs on numerous, varying platforms, including Windows, Linux, Unix, Mac OS X, and so on.
PHP is compatible with almost any modern server, such as Apache, IIS, and more.
PHP supports a wide range of databases.
PHP is free!
PHP is easy to learn and runs efficiently on the server side.
----------------------------------------------------------------------------------------------
PHP Syntax

A PHP script starts with <?php and ends with ?>:
<?php
// PHP code goes here
?>

Here is an example of a simple PHP file. The PHP script uses a built in function called "echo"
to output the text "Hello World!" to a web page.
<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<?php
echo "Hello World!";
?>
</body>
</html>

PHP statements end with semicolons (;).
-----------------------------------------------------------------------------------------------
PHP Syntax

Alternatively, we can include PHP in the HTML <script> tag.
<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<script language="php">
echo "Hello World!";
</script>
</body>
</html>

However, the latest version of PHP removes support for <script language="php"> tags. As such, we
recommend using <?php ?> exclusively.
--------------------------------------------------------------------------------------------------
PHP Syntax

You can also use the shorthand PHP tags, <? ?>, as long as they're supported by the server.
<?
echo "Hello World!";
?>
Try It Yourself

However, <?php ?>, as the official standard, is the recommended way of defining PHP scripts.
---------------------------------------------------------------------------------------------------
Echo

PHP has a built-in "echo" function, which is used to output text.
In actuality, it's not a function; it's a language construct. As such, it does not require parentheses.

Let's output a text.
<?php
echo "I love PHP!";
?>

The text should be in single or double quotation marks.
----------------------------------------------------------------------------------------------------
PHP Statements

Each PHP statement must end with a semicolon.
<?php
echo "A";
echo "B";
echo "C";
?>

Forgetting to add a semicolon at the end of a statement results in an error.
-----------------------------------------------------------------------------------------------------
Echo

HTML markup can be added to the text in the echo statement.
<?php
echo "<strong>This is a bold text.</strong>";
?>
------------------------------------------------------------------------------------------------------
Comments

In PHP code, a comment is a line that is not executed as part of the program. You can use comments to
communicate to others so they understand what you're doing, or as a reminder to yourself of what you did.

A single-line comment starts with //:
<?php
echo "<p>Hello World!</p>";
// This is a single-line comment
echo "<p>I am learning PHP!</p>";
echo "<p>This is my first program!</p>";
?>
--------------------------------------------------------------------------------------------------------
Multi-Line Comments

Multi-line comments are used for composing comments that take more than a single line.
A multi-line comment begins with /* and ends with */.
<?php
echo "<p>Hello World!</p>";
/*
This is a multi-line comment block
that spans over
multiple lines
*/
echo "<p>I am learning PHP!</p>";
echo "<p>This is my first program!</p>";
?>
Try It Yourself

Adding comments as you write your code is a good practice. It helps others understand your thinking
and makes it easier for you to recall your thought processes when you refer to your code later on.
-----------------------------------------------------------------------------------------------------------
Variables

Variables are used as "containers" in which we store information.
A PHP variable starts with a dollar sign ($), which is followed by the name of the variable.
$variable_name = value;

Rules for PHP variables:
- A variable name must start with a letter or an underscore
- 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 ($name and $NAME would be two different variables)

For example:
<?php
$name = 'John';
$age = 25;
echo $name;

// Outputs 'John'
?>

In the example above, notice that we did not have to tell PHP which data type the variable is.
PHP automatically converts the variable to the correct data type, depending on its value.
Unlike other programming languages, PHP has no command for declaring a variable. It is created the
moment you first assign a value to it.
------------------------------------------------------------------------------------------------------------
Constants

Constants are similar to variables except that they cannot be changed or undefined after they've been defined.
Begin the name of your constant with a letter or an underscore.
To create a constant, use the define() function:
define(name, value, case-insensitive)

Parameters:
name: Specifies the name of the constant;
value: Specifies the value of the constant;
case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false;

The example below creates a constant with a case-sensitive name:
<?php
define("MSG", "Hi SoloLearners!");
echo MSG;

// Outputs "Hi SoloLearners!"
?>

The example below creates a constant with a case-insensitive name:
<?php
define("MSG", " Hi SoloLearners!", true);
echo msg;

// Outputs "Hi SoloLearners!"
?>

No dollar sign ($) is necessary before the constant name.
------------------------------------------------------------------------------------------
Data Types

Variables can store a variety of data types.
Data types supported by PHP: String, Integer, Float, Boolean, Array, Object, NULL, Resource.

PHP String
A string is a sequence of characters, like "Hello world!"
A string can be any text within a set of single or double quotes.
<?php
$string1 = "Hello world!"; //double quotes
$string2 = 'Hello world!'; //single quotes
?>

You can join two strings together using the dot ( .) concatenation operator.
For example: echo $s1 . $s2


PHP Integer
An integer is a whole number (without decimals) that must fit the following criteria:
- It cannot contain commas or blanks
- It must not have a decimal point
- It can be either positive or negative
<?php
$int1 = 42; // positive number
$int2 = -42; // negative number
?>
---------------------------------------------------------------------------------------------
PHP Float

A float, or floating point number, is a number that includes a decimal point.
<?php
$x = 42.168;
?>

PHP Boolean

A Boolean represents two possible states: TRUE or FALSE.
<?php
$x = true; $y = false;
?>

Booleans are often used in conditional testing, which will be covered later on in the course.

Most of the data types can be used in combination with one another. In this example, string and
integer are put together to determine the sum of two numbers.
<?php
$str = "10";
$int = 20;
$sum = $str + $int;
echo ($sum);

// Outputs 30
?>
--------------------------------------------------------------------------------------------------
Variables Scope

PHP variables can be declared anywhere in the script.
The scope of a variable is the part of the script in which the variable can be referenced or used.

PHP's most used variable scopes are local, global.
A variable declared outside a function has a global scope, and can only be accessed outside of a function.
A variable declared within a function has a local scope, and can only be accessed within that function.

Consider the following example.
<?php
$name = 'David';
function getName() {
echo $name;
}
getName();

// Error: Undefined variable: name
?>
Try It Yourself

This script will produce an error, as the $name variable has a global scope, and is not accessible
within the getName() function. Functions will be discussed in the coming lessons.
------------------------------------------------------------------------------------------------------
The global Keyword

The global keyword is used to access a global variable from within a function.
To do this, use the global keyword within the function, prior to the variables.
<?php
$name = 'David';
function getName() {
global $name;
echo $name;
}
getName();

//Outputs 'David'
?>
--------------------------------------------------------------------------------------------------------
Variable Variables

With PHP, you can use one variable to specify another variable's name.
So, a variable variable treats the value of another variable as its name.

For example:
<?php
$a = 'hello';
$hello = "Hi!";
echo $$a;

// Outputs 'Hi!'
?>
Try It Yourself

$$a is a variable that is using the value of another variable, $a, as its name. The value of $a is
equal to "hello". The resulting variable is $hello, which holds the value "Hi!".
-------------------------------------------------------------------------------------------------------------
Operators

Operators carry out operations on variables and values.
Arithmetic Operators

Arithmetic operators work with numeric values to perform common arithmetical operations.

Example:
<?php
$num1 = 8;
$num2 = 6;

//Addition
echo $num1 + $num2; //14

//Subtraction
echo $num1 - $num2; //2

//Multiplication
echo $num1 * $num2; //48

//Division
echo $num1 / $num2; //1.33333333333
?>
---------------------------------------------------------------------------------------------------------------
Modulus

The modulus operator, represented by the % sign, returns the remainder of the division of the first operand by
the second operand:

<?php
$x = 14;
$y = 3;
echo $x % $y; // 2
?>

If you use floating point numbers with the modulus operator, they will be converted to integers before the operation.
----------------------------------------------------------------------------------------------------------------
Increment & Decrement

The increment operators are used to increment a variable's value.
The decrement operators are used to decrement a variable's value.
$x++; // equivalent to $x = $x+1;
$x--; // equivalent to $x = $x-1;

Increment and decrement operators either precede or follow a variable.
$x++; // post-increment
$x--; // post-decrement
++$x; // pre-increment
--$x; // pre-decrement

The difference is that the post-increment returns the original value before it changes the variable, while
the pre-increment changes the variable first and then returns the value.
Example:
$a = 2; $b = $a++; // $a=3, $b=2
$a = 2; $b = ++$a; // $a=3, $b=3
-------------------------------------------------------------------------------------------------------------------
Assignment Operators

Assignment operators work with numeric values to write values to variables.
$num1 = 5;
$num2 = $num1;

$num1 and $num2 now contain the value of 5.

Assignments can also be used in conjunction with arithmetic operators.

Example:
<?php
$x = 50;
$x += 100;
echo $x;

// Outputs: 150
?>
---------------------------------------------------------------------------------------------------------------------
Comparison Operators

Comparison operators compare two values (numbers or strings).
Comparison operators are used inside conditional statements, and evaluate to either TRUE or FALSE.
----------------------------------------------------------------------------------------------------------------------
Logical Operators

Logical operators are used to combine conditional statements.
----------------------------------------------------------------------------------------------------------------------
Arrays

An array is a special variable, which can hold more than one value at a time.
If you have a list of items (a list of names, for example), storing them in single variables would look like this:
$name1 = "David";
$name2 = "Amy";
$name3 = "John";

But what if you have 100 names on your list? The solution: Create an array!

Numeric Arrays
Numeric or indexed arrays associate a numeric index with their values.
The index can be assigned automatically (index always starts at 0), like this:
$names = array("David", "Amy", "John");

As an alternative, you can assign your index manually.
$names[0] = "David";
$names[1] = "Amy";
$names[2] = "John";

We defined an array called $names that stores three values.
You can access the array elements through their indices.
echo $names[1]; // Outputs "Amy"

Remember that the first element in an array has the index of 0, not 1.
-----------------------------------------------------------------------------------------------------------------------
Numeric Arrays

You can have integers, strings, and other data types together in one array.
Example:
<?php
$myArray[0] = "John";
$myArray[1] = "<strong>PHP</strong>";
$myArray[2] = 21;

echo "$myArray[0] is $myArray[2] and knows $myArray[1]";

// Outputs "John is 21 and knows PHP"
?>
------------------------------------------------------------------------------------------------------------------------
Associative Arrays

Associative arrays are arrays that use named keys that you assign to them.
There are two ways to create an associative array:
$people = array("David"=>"27", "Amy"=>"21", "John"=>"42");
// or
$people['David'] = "27";
$people['Amy'] = "21";
$people['John'] = "42";

In the first example, note the use of the => signs in assigning values to the named keys.
-------------------------------------------------------------------------------------------------------------------------
Associative Arrays

Use the named keys to access the array's members.
$people = array("David"=>"27", "Amy"=>"21", "John"=>"42");

echo $people['Amy']; // Outputs 21"
--------------------------------------------------------------------------------------------------------------------------
Multi-Dimensional Arrays

A multi-dimensional array contains one or more arrays.

The dimension of an array indicates the number of indices you would need to select an element.
- For a two-dimensional array, you need two indices to select an element
- For a three-dimensional array, you need three indices to select an element
Arrays more than three levels deep are difficult to manage.
----------------------------------------------------------------------------------------------------------------------------
Multi-Dimensional Arrays

Let's create a two-dimensional array that contains 3 arrays:
$people = array(
'online'=>array('David', 'Amy'),
'offline'=>array('John', 'Rob', 'Jack'),
'away'=>array('Arthur', 'Daniel')
);

Now the two-dimensional $people array contains 3 arrays, and it has two indices: row and column.
To access the elements of the $people array, we must point to the two indices.
echo $people['online'][0]; //Outputs "David"

echo $people['away'][1]; //Outputs "Daniel"

The arrays in the multi-dimensional array can be both numeric and associative.
------------------------------------------------------------------------------------------------------------------------------
Conditional Statements

Conditional statements perform different actions for different decisions.
The if else statement is used to execute a certain code if a condition is true, and another code if the condition is false.
Syntax:
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}

You can also use the if statement without the else statement, if you do not need to do anything, in case the condition is false.
---------------------------------------------------------------------------------------------------------------------------------
If Else

The example below will output the greatest number of the two.
<?php
$x = 10;
$y = 20;
if ($x >= $y) {
echo $x;
} else {
echo $y;
}

// Outputs "20"
?>
----------------------------------------------------------------------------------------------------------------------------------
The Elseif Statement

Use the if...elseif...else statement to specify a new condition to test, if the first condition is false.

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

You can add as many elseif statements as you want. Just note, that the elseif statement must begin with an if statement.
-----------------------------------------------------------------------------------------------------------------------------------
The Elseif Statement

For example:
<?php
$age = 21;

if ($age<=13) {
echo "Child.";
} elseif ($age>13 && $age<19) {
echo "Teenager";
} else {
echo "Adult";
}

//Outputs "Adult"
?>

We used the logical AND (&&) operator to combine the two conditions and check to determine whether $age is between 13 and 19.
------------------------------------------------------------------------------------------------------------------------------------
Loops

When writing code, you may want the same block of code to run over and over again. Instead of adding several almost equal
code-lines in a script, we can use loops to perform a task like this.

The while Loop

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;
}

If the condition never becomes false, the statement will continue to execute indefinitely.
-------------------------------------------------------------------------------------------------------------------------------------

The while Loop

The example below first sets a variable $i to one ($i = 1). Then, the while loop runs as long as $i is less than seven ($i < 7).
$i will increase by one each time the loop runs ($i++):

$i = 1;
while ($i < 7) {
echo "The value is $i <br />";
$i++;
}
--------------------------------------------------------------------------------------------------------------------------------------
The do...while Loop

The do...while loop will always execute the block of code once, check the condition, and repeat the loop as long as the specified
condition is true.

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

Regardless of whether the condition is true or false, the code will be executed at least once, which could be needed
in some situations.
-------------------------------------------------------------------------------------------------------------------------------------
The do...while Loop

The example below will write some output, and then increment the variable $i by one. Then the condition is checked,
and the loop continues to run, as long as $i is less than or equal to 7.
$i = 5;
do {
echo "The number is " . $i . "<br/>";
$i++;
} while($i <= 7);

//Output
//The number is 5
//The number is 6
//The number is 7

Note that in a do while loop, the condition is tested AFTER executing the statements within the loop. This means that the
do while loop would execute its statements at least once, even if the condition is false the first time.
--------------------------------------------------------------------------------------------------------------------------------------
The for Loop

The for loop is used when you know in advance how many times the script should run.
for (init; test; increment) {
code to be executed;
}

Parameters:
init: Initialize the loop counter value
test: Evaluates each time the loop is iterated, continuing if evaluates to true, and ending if it evaluates to false
increment: Increases the loop counter value
Each of the parameter expressions can be empty or contain multiple expressions that are separated with commas.
In the for statement, the parameters are separated with semicolons.
---------------------------------------------------------------------------------------------------------------------------------------
The for Loop

The example below displays the numbers from 0 to 5:
for ($a = 0; $a < 6; $a++) {
echo "Value of a : ". $a . "<br />";
}

The for loop in the example above first sets the variable $a to 0, then checks for the condition ($a < 6).
If the condition is true, it runs the code. After that, it increments $a ($a++).
--------------------------------------------------------------------------------------------------------------------------------------


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 2 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron