0% found this document useful (0 votes)
9 views

PHP Tutorial

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

PHP Tutorial

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

echo = eCHo = ECHO

$COLOR dif $coLOR


$x = 5 /* + 15 */ + 5;
$txt = "Hello world!";
$x = 5;
$y = 10.5;
echo "I love $txt!";
echo "I love " . $txt . "!";
echo $x + $y;

function myTest() {
global $x, $y;
$y = $x + $y;
}

function myTest() {
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}
myTest();

<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();
?>

<?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.";
?>

$x = 5985;
var_dump($x);

$cars = array("Volvo","BMW","Toyota");
var_dump($cars);

$cars = ["Volvo","BMW","Toyota"];
var_dump($cars);

<?php
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
public function message() {
return "My car is a " . $this->color . " " . $this->model . "!";
}
}
$myCar = new Car("black", "Volvo");
echo $myCar -> message();
echo "<br>";
$myCar = new Car("red", "Toyota");
echo $myCar -> message();
?>

<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>

echo strlen("Hello world!");


echo str_word_count("Hello world!");
echo strrev("Hello world!");
echo strpos("Hello world!", "world");
echo str_replace("world", "Dolly", "Hello world!");

$x = 5985;
var_dump(is_int($x));
$x = 59.85;
var_dump(is_int($x));
$x = 1.9e411;
var_dump($x);
$x = acos(8);
var_dump($x);

$x = 5985;
var_dump(is_numeric($x));
$x = "5985";
var_dump(is_numeric($x));
$x = "59.85" + 100;
var_dump(is_numeric($x));
$x = "Hello";
var_dump(is_numeric($x));

// Cast float to int


$x = 23465.768;
$int_cast = (int)$x;
echo $int_cast;

// Cast string to int


$x = "23465.768";
$int_cast = (int)$x;
echo $int_cast;

echo(pi());
echo pi();

echo(min(0, 150, 30, 20, -8, -200) . "<br>");


echo(max(0, 150, 30, 20, -8, -200));

echo min(0, 150, 30, 20, -8, -200) . "<br>";


echo max(0, 150, 30, 20, -8, -200);

echo(abs(-6.7));
echo abs(-6.7);

echo(sqrt(64));
echo(round(0.60)); // returns 1
echo(round(0.49)); // returns 0
echo(rand());
echo(rand(10, 100));

// case-sensitive constant name


define("GREETING", "Welcome to W3Schools.com!",true);
echo GREETINg;

define("cars", [
"Alfa Romeo",
"BMW",
"Toyota"
]);
echo cars[0];

Constants are automatically global and can be used across the entire script
<?php
define("GREETING", "Welcome to W3Schools.com!");
function myTest() {
echo GREETING;
}
myTest();
?>

$x % $y
(int)($x / $y);
$x ** $y
$x == $y
$x === $y
$x != $y diferite ca valoare
$x <> $y
$x !== $y diferite ca tip sau valoare
$x > $y
$x >= $y
$x <=> $y 1, 0 sau -1 in functie de x si y, pt x>y return 1

$x = 10;
echo ++$x; pre-increment, afis 11
$x++ post-increment, 10

$x and $y
$x or $y
$x xor $y - nu ambele true
$x && $y True if both $x and $y are true
$x || $y
!$x True if $x is not true
$txt1 . $txt2
$txt1 .= $txt2 Concatenation assignment

echo $user = $_GET["user"] ?? "anonymous";

<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>

<?php
$t = date("H");
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>

<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>

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

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

for ($x = 0; $x <= 10; $x++) {


echo "The number is: $x <br>";
}

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


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

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");


foreach($age as $x => $val) {
echo "$x = $val<br>";
}
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
break;
}
echo "The number is: $x <br>";
}

for ($x = 0; $x < 10; $x++) {


if ($x == 4) {
continue;
}
echo "The number is: $x <br>";
}

function writeMsg() {
echo "Hello world!";
}
writeMsg();

function familyName($fname) {
echo "$fname Refsnes.<br>";
}
familyName("Jani");

function familyName($fname, $year) {


echo "$fname Refsnes. Born in $year <br>";
}
familyName("Hege", "1975");

function addNumbers(int $a, int $b) {


return $a + $b;
}
echo addNumbers(5, "5 days");
// since strict is NOT enabled "5 days" is changed to int(5), and it will return 10

<?php declare(strict_types=1); // strict requirement


function addNumbers(int $a, int $b) {
return $a + $b;
}
echo addNumbers(5, "5 days");
// since strict is enabled and "5 days" is not an integer, an error will be thrown
?>

<?php declare(strict_types=1); // strict requirement


function setHeight(int $minheight = 50) {
echo "The height is : $minheight <br>";
}
setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
setHeight(80);
?>

<?php declare(strict_types=1); // strict requirement


function sum(int $x, int $y) {
$z = $x + $y;
return $z;
}
echo "5 + 10 = " . sum(5, 10) . "<br>";
echo "7 + 13 = " . sum(7, 13) . "<br>";
echo "2 + 4 = " . sum(2, 4);
?>

<?php declare(strict_types=1); // strict requirement


function addNumbers(float $a, float $b) : float {
return $a + $b;
}
echo addNumbers(1.2, 5.2);
?>

<?php declare(strict_types=1); // strict requirement


function addNumbers(float $a, float $b) : int {
return (int)($a + $b);
}
echo addNumbers(1.2, 5.2);
?>

<?php
function add_five(&$value) {
$value += 5;
}
$num = 2;
add_five($num);
echo $num;
?>

<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>

$cars = array("Volvo", "BMW", "Toyota");


echo count($cars);

$cars = array("Volvo", "BMW", "Toyota");


echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";

$cars = array("Volvo", "BMW", "Toyota");


$arrlength = count($cars);
for($x = 0; $x < $arrlength; $x++) {
echo $cars[$x];
echo "<br>";
}

The PHP superglobal variables are:


$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION

$_SERVER['PHP_SELF'] Returns the filename of the currently executing script


$_SERVER['GATEWAY_INTERFACE'] Returns the version of the Common Gateway Interface
(CGI) the server is using
$_SERVER['SERVER_ADDR'] Returns the IP address of the host server
$_SERVER['SERVER_NAME'] Returns the name of the host server (such as
www.w3schools.com)
$_SERVER['SERVER_SOFTWARE'] Returns the server identification string (such as
Apache/2.2.24)
$_SERVER['SERVER_PROTOCOL'] Returns the name and revision of the information
protocol (such as HTTP/1.1)
$_SERVER['REQUEST_METHOD'] Returns the request method used to access the page
(such as POST)
$_SERVER['REQUEST_TIME'] Returns the timestamp of the start of the request
(such as 1377687496)
$_SERVER['QUERY_STRING'] Returns the query string if the page is accessed via
a query string
$_SERVER['HTTP_ACCEPT'] Returns the Accept header from the current request
$_SERVER['HTTP_ACCEPT_CHARSET'] Returns the Accept_Charset header from the
current request (such as utf-8,ISO-8859-1)
$_SERVER['HTTP_HOST'] Returns the Host header from the current request
$_SERVER['HTTP_REFERER'] Returns the complete URL of the current page (not
reliable because not all user-agents support it)
$_SERVER['HTTPS'] Is the script queried through a secure HTTP protocol
$_SERVER['REMOTE_ADDR'] Returns the IP address from where the user is viewing the
current page
$_SERVER['REMOTE_HOST'] Returns the Host name from where the user is viewing the
current page
$_SERVER['REMOTE_PORT'] Returns the port being used on the user's machine to
communicate with the web server
$_SERVER['SCRIPT_FILENAME'] Returns the absolute pathname of the currently
executing script
$_SERVER['SERVER_ADMIN'] Returns the value given to the SERVER_ADMIN directive
in the web server configuration file (if your script runs on a virtual host, it
will be the value defined for that virtual host) (such as someone@w3schools.com)
$_SERVER['SERVER_PORT'] Returns the port on the server machine being used by the
web server for communication (such as 80)
$_SERVER['SERVER_SIGNATURE'] Returns the server version and virtual host name
which are added to server-generated pages
$_SERVER['PATH_TRANSLATED'] Returns the file system based path to the current
script
$_SERVER['SCRIPT_NAME'] Returns the path of the current script
$_SERVER['SCRIPT_URI'] Returns the URI of the current page

You might also like