php

PHP – Operators

PHP – Operators

PHP Arithmetic Operators

<?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 
?>

Assignement Operators

<?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
?>

String Operators

<?php
$a = "Hello";
$b = $a . " world!";
echo $b; // outputs Hello world! 

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

Increment/Decrement Operators

<?php
$x=10; 
echo ++$x; // outputs 11 first  -> the increment starts
echo ++$x; // outputs 12        -> the incrementt continues...

$y=10; 
echo $y++; // outputs 10 first  -> the value in the same
echo $y++; // outputs 11 second -> the increment starts

$z=5;
echo --$z; // outputs 4 first   -> the decrement starts
echo --$z; // outputs 3 second  -> the decrement continues...

$i=5;
echo $i--; // outputs 5 first  -> the value in the same
echo $i--; // outputs 4 second -> the decrement starts
?>

Comparison Operators

== 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 [php] <?php $x=100; // It is an integer number $y="100"; // It is a string var_dump($x == $y); // Output: bool(true) echo "<br>"; var_dump($x === $y); // Output: bool(false) echo "<br>"; var_dump($x != $y); // Output: bool(false) echo "<br>"; var_dump($x !== $y); // Output: bool(true) echo "<br>"; $a=50; $b=90; var_dump($a > $b); // Output: bool(false) echo "<br>"; var_dump($a < $b); // Output: bool(true) ?> [/php]

Logical Operators

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

Array Operators

+ 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

<?php
$x = array("a" => "red", "b" => "green"); 
$y = array("c" => "blue", "d" => "yellow"); 
$z = $x + $y;   
     
var_dump($z);        // Output: array(4) { ["a"]=> string(3) "red" ["b"]=> string(5) "green" ["c"]=> string(4) "blue" ["d"]=> string(6) "yellow" }

var_dump($x == $y);  // Output: bool(false) 
var_dump($x === $y); // Output: bool(false) 
var_dump($x != $y);  // Output: bool(true) 
var_dump($x <> $y);  // Output: bool(true) 
var_dump($x !== $y); // Output: bool(true) 
?>
By |PHP, Web Design|Commenti disabilitati su PHP – Operators

PHP – Constants

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).

Unlike variables, constants are automatically global across the entire script.

To set a constant, use the define() function.

Statement:

define(“GREETING”, “Welcome to W3Schools.com!”, true);

define(“constant name”, “constat value”, case-insensitive true/false);


<?php

// define a case-insensitive constant
define("GREETING", "Welcome to W3Schools.com!", true);
echo GREETING;
echo "<br>";
// will also output the value of the constant
echo greeting; // case-insensitive

?>  

By |PHP|Commenti disabilitati su PHP – Constants

PHP – Variables and Data Types

PHP – Variables and Data Types

Variables are containers for storing data

PHP has no command for declaring a variable.
A variable is created the moment you first assign a value to it.

Rules for PHP variables:

A variable starts with the $ sign, followed by the name of the variable
A variable name must start with a letter or the underscore character
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 ($y and $Y are two different variables)

Example:


<?php

$txt="Hello world!";
$x=5;
$y=10.5;

?>

Local and Global

A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function.

A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function.

global – Keyword

The global keyword is used to access a global variable from within a function.


<?php
$x=5;
$y=10;

function myTest()
{
global $x,$y; // It takes the global variables x and y 
$y=$x+$y;
}

myTest();
echo $y; // Outputs 15
?>

static – Keyword

Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job.

To do this, use the static keyword when you first declare the variable:


<?php

function myTest()
{
static $x=0; // the variable is static and it will not be deleted
echo $x;
$x++;
}

myTest(); // output is 0
echo "<br>";
myTest(); // output is 1
echo "<br>";
myTest(); // output is 2
echo "<br>";
myTest(); // output is 3
echo "<br>";
myTest(); // output is 4
?>

The result is:

0
1
2
3
4

Data Types

The types of data the variables can content.


<?php 

// STRINGS
// -------------------
$x = "Hello world!";
echo $x;
echo "<br>"; 
$x = 'Hello world!';
echo $x;

// INTEGERS
// -------------------
$x = 5985;
var_dump($x); // var_dump() function returns the data type and value of variables
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);

// FLOATING NUMBERS
// -------------------
$x = 10.365;
var_dump($x); // var_dump() function returns the data type and value of variables
echo "<br>"; 
$x = 2.4e3;
var_dump($x);
echo "<br>"; 
$x = 8E-5;
var_dump($x);

// BOOLEANS
// -------------------
$x=true;
$y=false;

// NULL
// -------------------
$x="Hello world!";
$x=null;
var_dump($x); 

?>

By |PHP|Commenti disabilitati su PHP – Variables and Data Types

PHP – Basic Structure

PHP – Basic Structure

PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.

PHP code are executed on the server, and the result is returned to the browser as plain HTML.

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

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

PHP files have extension “.php”

The official PHP website is: http://www.php.net

<!DOCTYPE html>
<html>
<body>

<?php

// This is a one-line c++ style comment

# This is a one-line shell-style comment

/* This is a multi line comment
yet another line of comment */

echo "Hello World!";
?>

</body>
</html>

Notice:
The PHP code is tagged inside:

<?php
... your php code ... ;
?>

or with the short notation
<?
... your php code ... ;
?>

By |PHP|Commenti disabilitati su PHP – Basic Structure

PHP Redirection – Multilingual Site

PHP Redirection code to create a Multilingual Site. This code is Ready to Put in your website.

By |PHP, Web Design|Commenti disabilitati su PHP Redirection – Multilingual Site