• March 28, 2024, 02:53:04 pm

Author Topic: Easy PHP Tutorials  (Read 8933 times)

0 Members and 2 Guests are viewing this topic.

Offline mk27

  • Developer
  • Forum User
  • ****
  • Posts: 79
  • Reputation: 29
  • I am alive and exist on Discord. PM for add.
  • Current Phone: : Nokia X7-00
Easy PHP Tutorials
« on: July 22, 2013, 06:52:10 am »
PHP is really Easy. It's the most Popular programming language for dynamic generated pages on websites. So it is very useful for creating dynamic generated pages.

Here is an example of hello world:
Quote
<?php
echo "Hello world";
?>

so first line tells PHP engine that PHP tag is open and last line closes that. So everything between <?php and ?> is going to be treated as PHP commands. On second line i placed a command/function named echo which outputs the text. The semicolon is very important to end PHP statement. Also in PHP there are variables that start like $this.
Now take a look at this example:
Quote
<?php
$variablename = "Hello World";
echo $variablename;
// also this is how to place comment in script.
?>
this will output same thing.
Ok. Files with PHP scripts can be saved as .php files. It can simply be run by opening URL of that page in browser.
To run these script you can simply upload them to PHP supported website directory or make your own web-server with xampp and run them using your computer.



I can't explain everything here now so I am giving you links where you can get knowledge of PHP.

Here are the links where you can learn PHP:
http://w3schools.com/php/default.asp
http://www.php.net
http://www.youtube.com/user/phpacademy
http://www.youtube.com/playlist?list=PL442FA2C127377F07

I recommend you to start off with 4-th link. Because it's very easy.

Have fun :) remember that it's easy. I Will update this post and add more details later.
« Last Edit: July 22, 2013, 03:46:59 pm by mrkenkadze27 »

Offline mk27

  • Developer
  • Forum User
  • ****
  • Posts: 79
  • Reputation: 29
  • I am alive and exist on Discord. PM for add.
  • Current Phone: : Nokia X7-00
Re: Easy PHP Tutorials
« Reply #1 on: August 27, 2013, 09:43:54 pm »
What You Should Already Know

Before you continue you should have a basic understanding of the following:
    HTML
    CSS
    JavaScript

What is PHP?
    PHP is an acronym for Hypertext Preprocessor
    PHP is a widely-used, open source scripting language
    PHP scripts are executed on the server
    PHP costs nothing, it is free to download and use

PHP is simple for beginners.
PHP also offers many advanced features for professional programmers.

What is a PHP File?
    PHP files can contain text, HTML, CSS, JavaScript, and PHP code
    PHP code are executed on the server, and the result is returned to the browser as plain HTML
    PHP files have extension ".php"

What Can PHP Do?
    PHP can generate dynamic page content
    PHP can create, open, read, write, and close files on the server
    PHP can collect form data
    PHP can send and receive cookies
    PHP can add, delete, modify data in your database
    PHP can restrict users to access some pages on your website
    PHP can encrypt data

With PHP you are not limited to output HTML. You can output images, PDF files, and even Flash movies. You can also output any text, such as XHTML and XML.
Why PHP?
    PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
    PHP is compatible with almost all servers used today (Apache, IIS, etc.)
    PHP supports a wide range of databases
    PHP is free. Download it from the official PHP resource: www.php.net
    PHP is easy to learn and runs efficiently on the server side


What Do I Need?

To start using PHP, you can:
    Find a web host with PHP and MySQL support
    Install a web server on your own PC, and then install PHP and MySQL

Use a Web Host With PHP Support
If your server has activated support for PHP you do not need to do anything.
Just create some .php files, place them in your web directory, and the server will automatically parse them for you.
You do not need to compile anything or install any extra tools.
Because PHP is free, most web hosts offer PHP support.
Set Up PHP on Your Own PC

However, if your server does not support PHP, you must:
    install a web server
    install PHP
    install a database, such as MySQL

The official PHP website (PHP.net) has installation instructions for PHP: http://php.net/manual/en/install.php
« Last Edit: August 27, 2013, 09:46:27 pm by mrkenkadze27 »

Offline mk27

  • Developer
  • Forum User
  • ****
  • Posts: 79
  • Reputation: 29
  • I am alive and exist on Discord. PM for add.
  • Current Phone: : Nokia X7-00
Re: Easy PHP Tutorials
« Reply #2 on: August 27, 2013, 09:49:33 pm »
The PHP script is executed on the server, and the plain HTML result is sent back to the browser.
Basic PHP Syntax

A PHP script can be placed anywhere in the document.

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

The default file extension for PHP files is ".php".

A PHP file normally contains HTML tags, and some PHP scripting code.

Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP function "echo" to output the text "Hello World!" on a web page:
Example
Code: [Select]
<!DOCTYPE html>
<html>
<body>

<h1>My first PHP page</h1>

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


</body>
</html>

Note: PHP statements are terminated by semicolon (;). The closing tag of a block of PHP code also automatically implies a semicolon (so you do not have to have a semicolon terminating the last line of a PHP block).

Comments in PHP

A comment in PHP code is a line that is not read/executed as part of the program. Its only purpose is to be read by someone who is editing the code!
Comments are useful for:
    To let others understand what you are doing - Comments let other programmers understand what you were doing in each step (if you work in a group)
    To remind yourself what you did - Most programmers have experienced coming back to their own work a year or two later and having to re-figure out what they did. Comments can remind you of what you were thinking when you wrote the code

PHP supports three ways of commenting:
Example
Code: [Select]
<!DOCTYPE html>
<html>
<body>

<?php
// This is a single line comment

# This is also a single line comment

/*
This is a multiple lines comment block
that spans over more than
one line
*/
?>


</body>
</html>


PHP Case Sensitivity

In PHP, all user-defined functions, classes, and keywords (e.g. if, else, while, echo, etc.) are case-insensitive.

In the example below, all three echo statements below are legal (and equal):
Example
Code: [Select]
<!DOCTYPE html>
<html>
<body>

<?php
ECHO "Hello World!<br>";
echo 
"Hello World!<br>";
EcHo 
"Hello World!<br>";
?>


</body>
</html>


However; in PHP, all variables are case-sensitive.

In the example below, only the first statement will display the value of the $color variable (this is because $color, $COLOR, and $coLOR are treated as three different variables):
Example
Code: [Select]
<!DOCTYPE html>
<html>
<body>

<?php
$color
="red";
echo 
"My car is " $color "<br>";
echo 
"My house is " $COLOR "<br>";
echo 
"My boat is " $coLOR "<br>";
?>


</body>
</html>
« Last Edit: August 27, 2013, 09:52:00 pm by mrkenkadze27 »

Offline mk27

  • Developer
  • Forum User
  • ****
  • Posts: 79
  • Reputation: 29
  • I am alive and exist on Discord. PM for add.
  • Current Phone: : Nokia X7-00
Re: Easy PHP Tutorials
« Reply #3 on: August 27, 2013, 09:55:38 pm »

Offline matthew

  • Mass Poster
  • ****
  • Posts: 1,315
  • Reputation: 13
  • SymphonyOS
  • Current Phone: :
    N8-00 (25.007)
    808 (113.010.1508)
    Retired E72, E6-00
Re: Easy PHP Tutorials
« Reply #4 on: August 29, 2013, 06:57:11 am »
Thank you for posting these introductions to programming, mate.
i appreciate your time  :)

Offline mk27

  • Developer
  • Forum User
  • ****
  • Posts: 79
  • Reputation: 29
  • I am alive and exist on Discord. PM for add.
  • Current Phone: : Nokia X7-00
Re: Easy PHP Tutorials
« Reply #5 on: September 11, 2013, 08:04:03 pm »
PHP 5 echo and print Statements

In PHP there is two basic ways to get output: echo and print.

In this tutorial we use echo (and print) in almost every example. So, this chapter contains a little more info about those two output statements.
PHP echo and print Statements

There are some difference between echo and print:

    echo - can output one or more strings
    print - can only output one string, and returns always 1

Tip: echo is marginally faster compare to print as echo does not return any value.


The PHP echo Statement

echo is a language construct, and can be used with or without parantheses: echo or echo().

Display Strings

The following example shows how to display different strings with the echo command (also notice that the strings can contain HTML markup):
Example
Code: [Select]
<?php
echo "<h2>PHP is fun!</h2>";
echo 
"Hello world!<br>";
echo 
"I'm about to learn PHP!<br>";
echo 
"This"" string"" was"" made"" with multiple parameters.";
?>

Display Variables

The following example shows how to display strings and variables with the echo command:
Example
Code: [Select]
<?php
$txt1
="Learn PHP";
$txt2="W3Schools.com";
$cars=array("Volvo","BMW","Toyota");

echo 
$txt1;
echo 
"<br>";
echo 
"Study PHP at $txt2";
echo 
"My car is a {$cars[0]}";
?>



The PHP print Statement

print is also a language construct, and can be used with or without parantheses: print or print().

Display Strings

The following example shows how to display different strings with the print command (also notice that the strings can contain HTML markup):
Example
Code: [Select]
<?php
print "<h2>PHP is fun!</h2>";
print 
"Hello world!<br>";
print 
"I'm about to learn PHP!";
?>

Display Variables

The following example shows how to display strings and variables with the print command:
Example
Code: [Select]
<?php
$txt1
="Learn PHP";
$txt2="W3Schools.com";
$cars=array("Volvo","BMW","Toyota");

print 
$txt1;
print 
"<br>";
print 
"Study PHP at $txt2";
print 
"My car is a {$cars[0]}";
?>

Offline mk27

  • Developer
  • Forum User
  • ****
  • Posts: 79
  • Reputation: 29
  • I am alive and exist on Discord. PM for add.
  • Current Phone: : Nokia X7-00
Re: Easy PHP Tutorials
« Reply #6 on: September 11, 2013, 08:07:05 pm »
PHP Data Types

String, Integer, Floating point numbers, Boolean, Array, Object, NULL.
PHP Strings

A string is a sequence of characters, like "Hello world!".

A string can be any text inside quotes. You can use single or double quotes:
Example
Code: [Select]
<?php
$x 
"Hello world!";
echo 
$x;
echo 
"<br>";
$x 'Hello world!';
echo 
$x;
?>


PHP Integers

An integer is a number without decimals.

Rules for integers:

    An integer must have at least one digit (0-9)
    An integer cannot contain comma or blanks
    An integer must not have a decimal point
    An integer can be either positive or negative
    Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0)

In the following example we will test different numbers. The PHP var_dump() function returns the data type and value of variables:
Example
Code: [Select]
<?php
$x 
5985;
var_dump($x);
echo 
"<br>";
$x = -345// negative number
var_dump($x);
echo 
"<br>";
$x 0x8C// hexadecimal number
var_dump($x);
echo 
"<br>";
$x 047// octal number
var_dump($x);
?>


PHP Floating Point Numbers

A floating point number is a number with a decimal point or a number in exponential form.

In the following example we will test different numbers. The PHP var_dump() function returns the data type and value of variables:
Example
Code: [Select]
<?php
$x 
10.365;
var_dump($x);
echo 
"<br>";
$x 2.4e3;
var_dump($x);
echo 
"<br>";
$x 8E-5;
var_dump($x);
?>


PHP Booleans

Booleans can be either TRUE or FALSE.
var x=true;
var y=false;

Booleans are often used in conditional testing. You will learn more about conditional testing in a later chapter of this tutorial.
PHP Arrays

An array stores multiple values in one single variable.

In the following example we create an array, and then use the PHP var_dump() function to return the data type and value of the array:
Example
Code: [Select]
<?php
$cars
=array("Volvo","BMW","Toyota");
var_dump($cars);
?>


You will learn a lot more about arrays in later chapters of this tutorial.


PHP Objects

An object is a data type which stores data and information on how to process that data.

In PHP, an object must be explicitly declared.

First we must declare a class of object. For this, we use the class keyword. A class is a structure that can contain properties and methods.

We then define the data type in the object class, and then we use the data type in instances of that class:
Example
Code: [Select]
<?php
class Car
{
  var 
$color;
  function 
Car($color="green")
  {
    
$this->color $color;
  }
  function 
what_color()
  {
    return 
$this->color;
  }
}
?>


You will learn more about objects in a later chapter of this tutorial.
PHP NULL Value

The special NULL value represents that a variable has no value. NULL is the only possible value of data type NULL.

The NULL value identifies whether a variable is empty or not. Also useful to differentiate between the empty string and null values of databases.

Variables can be emptied by setting the value to NULL:
Example
Code: [Select]
<?php
$x
="Hello world!";
$x=null;
var_dump($x);
?>

Offline mk27

  • Developer
  • Forum User
  • ****
  • Posts: 79
  • Reputation: 29
  • I am alive and exist on Discord. PM for add.
  • Current Phone: : Nokia X7-00
Re: Easy PHP Tutorials
« Reply #7 on: September 11, 2013, 08:09:44 pm »
PHP String Functions

A string is a sequence of characters, like "Hello world!".
PHP String Functions

In this chapter we will look at some commonly used functions to manipulate strings.
The PHP strlen() function

The strlen() function returns the length of a string, in characters.

The example below returns the length of the string "Hello world!":
Example
Code: [Select]
<?php
echo strlen("Hello world!");
?>

The output of the code above will be: 12

Tip: strlen() is often used in loops or other functions, when it is important to know when a string ends. (i.e. in a loop, we might want to stop the loop after the last character in a string).
The PHP strpos() function

The strpos() function is used to search for a specified character or text within a string.

If a match is found, it will return the character position of the first match. If no match is found, it will return FALSE.

The example below searches for the text "world" in the string "Hello world!":
Example
Code: [Select]
<?php
echo strpos("Hello world!","world");
?>

The output of the code above will be: 6.

Tip: The position of the string "world" in the example above is 6. The reason that it is 6 (and not 7), is that the first character position in the string is 0, and not 1.
Complete PHP String Reference

For a complete reference of all string functions, go to our complete PHP String Reference.

The PHP string reference contains description and example of use, for each function!

Offline mk27

  • Developer
  • Forum User
  • ****
  • Posts: 79
  • Reputation: 29
  • I am alive and exist on Discord. PM for add.
  • Current Phone: : Nokia X7-00
Re: Easy PHP Tutorials
« Reply #8 on: September 11, 2013, 08:12:44 pm »
PHP Constants

Constants are like variables except that once they are defined they cannot be changed or undefined.
PHP Constants

A constant is an identifier (name) for a simple value. The value cannot be changed during the script.

A valid constant name starts with a letter or underscore (no $ sign before the constant name).

Note: Unlike variables, constants are automatically global across the entire script.
Set a PHP Constant

To set a constant, use the define() function - it takes three parameters: The first parameter defines the name of the constant, the second parameter defines the value of the constant, and the optional third parameter specifies whether the constant name should be case-insensitive. Default is false.

The example below creates a case-sensitive constant, with the value of "Welcome to W3Schools.com!":
Example
Code: [Select]
<?php
define
("GREETING""Welcome to W3Schools.com!");
echo 
GREETING;
?>

The example below creates a case-insensitive constant, with the value of "Welcome to W3Schools.com!":
Example
Code: [Select]
<?php
define
("GREETING""Welcome to W3Schools.com!"true);
echo 
greeting;
?>

Offline mk27

  • Developer
  • Forum User
  • ****
  • Posts: 79
  • Reputation: 29
  • I am alive and exist on Discord. PM for add.
  • Current Phone: : Nokia X7-00
Re: Easy PHP Tutorials
« Reply #9 on: November 28, 2013, 04:49:04 pm »
PHP Operators

This chapter shows the different operators that can be used in PHP scripts.
PHP Arithmetic Operators
Operator    Name    Example    Result
+    Addition    $x + $y    Sum of $x and $y
-    Subtraction    $x - $y    Difference of $x and $y
*    Multiplication    $x * $y    Product of $x and $y
/    Division    $x / $y    Quotient of $x and $y
%    Modulus    $x % $y    Remainder of $x divided by $y

The example below shows the different results of using the different arithmetic operators:
Code: [Select]
<?php
$x
=10;
$y=6;
echo (
$x $y); // outputs 16
echo ($x $y); // outputs 4
echo ($x $y); // outputs 60
echo ($x $y); // outputs 1.6666666666667
echo ($x $y); // outputs 4
?>



PHP Assignment Operators

The PHP assignment operators is used to write a value to a variable.

The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.
Assignment    Same as...    Description
x = y    x = y    The left operand gets set to the value of the expression on the right
x += y    x = x + y    Addition
x -= y    x = x - y    Subtraction
x *= y    x = x * y    Multiplication
x /= y    x = x / y    Division
x %= y    x = x % y    Modulus

The example below shows the different results of using the different assignment operators:
Code: [Select]
<?php
$x
=10;
echo 
$x// outputs 10

$y=20;
$y += 100;
echo 
$y// outputs 120

$z=50;
$z -= 25;
echo 
$z// outputs 25

$i=5;
$i *= 6;
echo 
$i// outputs 30

$j=10;
$j /= 5;
echo 
$j// outputs 2

$k=15;
$k %= 4;
echo 
$k// outputs 3
?>



PHP String Operators
Operator    Name    Example    Result
.    Concatenation    $txt1 = "Hello"
$txt2 = $txt1 . " world!"    Now $txt2 contains "Hello world!"
.=    Concatenation assignment    $txt1 = "Hello"
$txt1 .= " world!"    Now $txt1 contains "Hello world!"

The example below shows the results of using the string operators:
Code: [Select]
<?php
$a 
"Hello";
$b $a " world!";
echo 
$b// outputs Hello world!

$x="Hello";
$x .= " world!";
echo 
$x// outputs Hello world!
?>



PHP Increment / Decrement Operators
Operator    Name    Description
++$x    Pre-increment    Increments $x by one, then returns $x
$x++    Post-increment    Returns $x, then increments $x by one
--$x    Pre-decrement    Decrements $x by one, then returns $x
$x--    Post-decrement    Returns $x, then decrements $x by one

The example below shows the different results of using the different increment/decrement operators:
Code: [Select]
<?php
$x
=10;
echo ++
$x// outputs 11

$y=10;
echo 
$y++; // outputs 10

$z=5;
echo --
$z// outputs 4

$i=5;
echo 
$i--; // outputs 5
?>



PHP Comparison Operators

The PHP comparison operators are used to compare two values (number or string):
Operator    Name    Example    Result
==    Equal    $x == $y    True if $x is equal to $y
===    Identical    $x === $y    True if $x is equal to $y, and they are of the same type
!=    Not equal    $x != $y    True if $x is not equal to $y
<>    Not equal    $x <> $y    True if $x is not equal to $y
!==    Not identical    $x !== $y    True if $x is not equal to $y, or they are not of the same type
>    Greater than    $x > $y    True if $x is greater than $y
<    Less than    $x < $y    True if $x is less than $y
>=    Greater than or equal to    $x >= $y    True if $x is greater than or equal to $y
<=    Less than or equal to    $x <= $y    True if $x is less than or equal to $y

The example below shows the different results of using some of the comparison operators:
Code: [Select]
<?php
$x
=100;
$y="100";

var_dump($x == $y);
echo 
"<br>";
var_dump($x === $y);
echo 
"<br>";
var_dump($x != $y);
echo 
"<br>";
var_dump($x !== $y);
echo 
"<br>";

$a=50;
$b=90;

var_dump($a $b);
echo 
"<br>";
var_dump($a $b);
?>



PHP Logical Operators
Operator    Name    Example    Result
and    And    $x and $y    True if both $x and $y are true
or    Or    $x or $y    True if either $x or $y is true
xor    Xor    $x xor $y    True if either $x or $y is true, but not both
&&    And    $x && $y    True if both $x and $y are true
||    Or    $x || $y    True if either $x or $y is true
!    Not    !$x    True if $x is not true


PHP Array Operators

The PHP array operators are used to compare arrays:
Operator    Name    Example    Result
+    Union    $x + $y    Union of $x and $y (but duplicate keys are not overwritten)
==    Equality    $x == $y    True if $x and $y have the same key/value pairs
===    Identity    $x === $y    True if $x and $y have the same key/value pairs in the same order and of the same types
!=    Inequality    $x != $y    True if $x is not equal to $y
<>    Inequality    $x <> $y    True if $x is not equal to $y
!==    Non-identity    $x !== $y    True if $x is not identical to $y

The example below shows the different results of using the different array operators:
Code: [Select]
<?php
$x 
= array("a" => "red""b" => "green");
$y = array("c" => "blue""d" => "yellow");
$z $x $y// union of $x and $y
var_dump($z);
var_dump($x == $y);
var_dump($x === $y);
var_dump($x != $y);
var_dump($x <> $y);
var_dump($x !== $y);
?>