+ Reply to Thread
Results 1 to 1 of 1
  1. [TUTORIAL] PHP #5 Structuri de control

    #1
    VIP ~TraNda~'s Avatar
    Member since
    Feb 2014
    Location
    WarGods
    Posts
    12,099
    Last username
    LiOn. ~JoK3r ~
    Blog Entries
    4
    Mentioned
    95 Post(s)
    Achievements Days Registered 4 Mentions Received 2 Mentions Received 1 Mentions Given 4 Mentions Given 3 Mentions Given 2 Mentions Given 1 Threads Rating Received 1
    Doneaza
    Sustine Comunitatea WarGods!
    Fii unul de-al nostru! Doneaza
    Doneaza in Cont Bancar
    Click aici pentru a face o Donatie

    Question [TUTORIAL] PHP #5 Structuri de control

    Aceste structuri permit definirea unei conditii si apoi executarea unor operatii functie de raspuns.

    Vom discuta despre:
    Code:
    You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!
    if este instructiunea conditionala cea mai folosita avand urmatoarea sintaxa:
    Code:
    You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!
    Conditia este incadrata de paranteze rotunde iar instructiunea care se executa in cazul in care conditia este indeplinita intre acolade. Neindeplinirea conditiei face sa se execute ce urmeaza dupa inchiderea acoladei.

    Exemplu: se compara doua variabile si se afiseaza un mesaj
    PHP Code:
    <?php
    $a
    =4;
    $b=3;
    echo 
    "$a=4<br>";
    echo 
    "$b=3<br>";
    if (
    $a>$b) {
    echo 
    "$a>$b";
    }
    ?>
    else este alta instructiunea conditionala care indica ce se executa in cazul in care conditia mentionata in instructiunea if nu este indeplinita si are sintaxa:
    Code:
    You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!
    Exemplu: se compara doua variabile si se afiseaza un mesaj
    PHP Code:
    <?php
    $a
    =2;
    $b=9;
    echo 
    "$a=2<br>";
    echo 
    "$b=9<br>";
    if (
    $a>$b) {
    echo 
    "$a>$b";
    } else {
    echo 
    "$a<$b";
    }
    ?>
    elseif este o combinatie intre if si else. In cazul in care conditia if nu este indeplinita se introduce elseif care testeaza inca o conditie. Daca nu este indeplinita a doua conditie se executa declaratia introdusa prin else.

    Code:
    You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!
    Exemplu:
    PHP Code:
    <?php
    $a
    =19;
    $b=23;
    if (
    $a<$b) {
    echo 
    "$a<$b";
    } elseif (
    $a==$b) {
    echo 
    "$a==$b";
    } else {
    echo 
    "$a>$b";
    }
    ?>
    switch este asemanatoare functiei if dar conditia are mai mult de doua valori.

    Exemplu: transforma numarul zilei in numele ei
    PHP Code:
    <?php
    $ziua
    =2;
    echo 
    $ziua;
    echo 
    "<br>";
    switch (
    $ziua) {
    case 
    1:
    echo 
    "Luni";
    break;
    case 
    2:
    echo 
    "Marti";
    break;
    case 
    3:
    echo 
    "Miercuri";
    break;
    case 
    4:
    echo 
    "Joi";
    break;
    case 
    5:
    echo 
    "Vineri";
    break;
    case 
    6:
    echo 
    "Sambata";
    break;
    case 
    7:
    echo 
    "Duminica";
    break;
    }
    ?>
    while este o instructiune de tip bucla. Atat timp cat conditia este adevarata se repeta bucla.

    Exemplu:
    PHP Code:
    <?php
    $numar
    =1;
    while (
    $numar<=7) {
    echo 
    $numar."<br>";
    $numar++;
    }
    ?>
    for este o instructiune de tip bucla.
    Code:
    You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!
    expresia1 este evaluata la inceputul buclei
    expresia2 se verifica la inceputul fiecarei iteratii
    expresia3 se executa la sfarsitul fiecarei iteratii
    constructia for poate functiona si fara una sau toate aceste expresii

    Exemplu: se initializeaza variabila i cu valoarea 1, se verifica daca este mai mica sau egala cu 10 iar apoi se incrementeaza. La fiecare iteratie se tipareste variabila i.
    PHP Code:
    <?php
    for ($i=1$i<=10$i++) {
    echo 
    $i."<br>";
    }
    ?>
    foreach este o constructie care functioneaza doar cu matrice generand erori cand sunt folosite variabile cu tipuri de date diferite sau variabile neinitializate. Sintaxa folosita este:
    Code:
    You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!
    Exemplu:
    PHP Code:
    <?php
    $personal 
    = array(
    "Florin" => director,
    "Catalin" => inginer,
    "Mihai" => economist,
    "Sorin" => sofer,
    "Diana" => secretara
    );

    foreach (
    $personal as $nume => $meserie) {
    echo 
    "$nume => $meserie<br>";
    }
    ?>
    include si require sunt doua functii asemanatoare folosite pentru includerea in paginile php a unor fisiere externe. Diferenta intre cele doua functii consta in faptul ca daca include da gres scriptul genereaza o avertizare dar functioneaza in continuare in timp ce la require se termina executarea scriptului.

    include "fisier1.php";
    require "fisier2.html";

    break opreste fortat executia structurilor for, foreach, while, do..while sau switch.
    break accepta optional un argument numeric care indica numarul de structuri imbricate a caror functionare este oprita.

    Exemplu: la i=6 se opreste executia buclei
    PHP Code:
    <?php
    for ($i=0; ;$i++) {
    if (
    $i>6) {
    break;
    }
    echo 
    $i."<br>";
    }
    ?>
    continue sare peste restul din iteratia buclei curente si continua executia la inceputul iteratiei urmatoare
    continue accepta optional un argument numeric care indica numarul de bucle care vor fi sarite pana la sfarsit.

    Exemplu: sare executia peste i==5
    PHP Code:
    <?php
    for ($i=0;$i<8;$i++) {
    if (
    $i==5)
    continue;
    echo 
    $i."<br>";
    ?>
    return
    Daca este chemata din interiorul unei functii declaratia return() opreste imediat executia functiei curente si furnizeaza argumentul ca valoare a functiei.
    Daca este chemata in scop global executia scriptului curent se opreste.

    Last edited by ~TraNda~; 11-04-2016 at 06:03 PM.
    -------------

    TraNda - WarGods | R.R.M - AngeL - LiOn. - pichacku - Jok3r - UnicA

    P A C E

    DISCORD: unknown.cfg


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Partners
Humble Monthly Bundle
Voucher PC-Garage