AgerNic.com/ro
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL
Tutorial

Tutorial PHP» PHP Operatori

Operatori PHP
Operatorii sunt folosi?i pentru a efectua opera?ii cu variabile ?i valori.

PHP împarte operatorii în urm?toarele grupuri:

Operatori de aritmetic?
Operatori de atribuire
Operatori de compara?ie
Operatori de cre?tere / descre?tere
Operatori logici
Operatori cu ?iruri
Operatori de matrice
Operatori de atribuire condi?ionat?

Exemplu:

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

echo $x + $y."<br />";
echo $x - $y."<br />";
echo $x * $y."<br />";
echo $x / $y."<br />";
echo $x % $y."<br />";
echo $x ** $y."<br />";
$x = 20;
$x += 100;
echo $x."<br />";
?>
<?php
$x = 50;
$y = 50;

var_dump($x >= $y); // returns true because $x is greater than or equal to $y
?>

Putem traversa un tabel indexat folosind bucle în PHP.
Putem face bucl? prin tabelul indexat în dou? moduri. În primul rând utilizarea prin bucl? ?i în al doilea rând prin utilizarea "foreach'.
Exemplu:

<?php 

// Creating an indexed array 
$name_one = array("Ion", "Anton", "Radu", "Vasilica", "Gheorghe", "Cristinel"); 
// array usign foreach 
echo "Bucla folosind 'foreach': <br />"; 
foreach ($name_one as $val){ 
echo $val. "<br />"; 
} 

// count() func?ia este utilizat? pentru num?rare 
// num?rul de elemente dintr-un tablou
$numar_elemente = count($name_one); 
echo "<br />Num?rul de elemente este $numar_elemente <br /> <br />"; 

// Un alt mod de a folosi bucla
echo " 'In bucl?' folosita pentru: <br />"; 
for($n = 0; $n < $numar_elemente; $n++){ 
echo $name_one[$n], "<br />"; 
} 

?>

Arrayuri asociative:

Acest tip de tablouri sunt similare cu matricele indexate, dar, în loc de stocare liniar?, fiecare valoare poate fi atribuit? cu o cheie definit? de utilizator de tip string.

Exemplu:


<?php 

// O modalitate de a crea un tablou asociativ 
$name_one = array("Zap"=>"Zapi", "Anthony"=>"Anyta", 
"Radu"=>"Raducu", "Sadicu"=>"Sadi", 
"Raluca"=>"Ralu"); 

// Al doilea mod de a crea un tablou asociativ 
$name_two["zap"] = "zapi"; 
$name_two["anthony"] = "anyta"; 
$name_two["radu"] = "raducu"; 
$name_two["sadicu"] = "sadi"; 
$name_two["raluca"] = "ralu"; 

// Accesarea direct? a elementelor
echo "Accesarea direct? a elementelor:<br />"; 
echo $name_two["zap"], "<br />"; 
echo $name_two["sadicu"], "<br />"; 
echo $name_two["anthony"], "<br />"; 
echo $name_one["Radu"], "<br />"; 
echo $name_one["Raluca"], "<br />"; 

?>

Traversing Associative Arrays

Putem traversa matricile asociative într-un mod similar, a?a cum s-a întâmplat în matricile numerice folosind bucle. Putem 'bucla' prin tabloul asociativ în dou? moduri. În primul rând prin utilizarea pentru bucl? ?i în al doilea rând prin utilizarea 'forech'.

<?php 

// Creating an associative array 
$name_one = array("Zack"=>"Zara", "Anthony"=>"Any", 
"Ram"=>"Rani", "Salim"=>"Sara", 
"Raghav"=>"Ravina"); 

// Looping through an array using foreach 
echo "Looping using foreach: \n"; 
foreach ($name_one as $val => $val_value){ 
echo "Husband is ".$val." and Wife is ".$val_value."\n"; 
} 

// Looping through an array using for 
echo "\nLooping using for: \n"; 
$keys = array_keys($name_two); 
$round = count($name_two); 

for($i=0; $i < $round; ++$i) { 
echo $keys[$i] . ' ' . $name_two[$keys[$i]] . "\n"; 
} 

?>

Arrayuri multidimensionale.

Matricele multidimensionale sunt astfel de tablouri care stocheaz? un alt tablou la fiecare index în loc de un singur element. Cu alte cuvinte, putem defini matricile multidimensionale ca tablou de matrice. Dup? cum sugereaz? ?i numele, fiecare element din acest tablou poate fi un tablou ?i pot de?ine ?i alte sub-tablouri în interior. Pot fi accesate matricile sau sub-matricile din tablouri multidimensionale folosind mai multe dimensiuni.
Exemplu:

<?php 

// Defining a multidimensional array 
$favorites = array( 
array( 
"name" => "Dave Punk", 
"mob" => "5689741523", 
"email" => "davepunk@gmail.com", 
), 
array( 
"name" => "Monty Smith", 
"mob" => "2584369721", 
"email" => "montysmith@gmail.com", 
), 
array( 
"name" => "John Flinch", 
"mob" => "9875147536", 
"email" => "johnflinch@gmail.com", 
) 
); 

// Accessing elements 
echo "Dave Punk email-id is: " . $favorites[0]["email"], "<br />"; 
echo "John Flinch mobile number is: " . $favorites[2]["mob"]; 

?>
Tags: PHP operatori, exemple de cod, script PHP gratis online

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


analytics piwika