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

PHP - Limbaj de Programare: Task!

This document provides an introduction to PHP, including: - PHP code is contained within <?php ?> tags - Common PHP functions like echo, print, and variables - Using servers and compilers to run PHP code - Operators, conditions, loops, arrays, and including other files - Interacting with databases and retrieving data from tables

Uploaded by

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

PHP - Limbaj de Programare: Task!

This document provides an introduction to PHP, including: - PHP code is contained within <?php ?> tags - Common PHP functions like echo, print, and variables - Using servers and compilers to run PHP code - Operators, conditions, loops, arrays, and including other files - Interacting with databases and retrieving data from tables

Uploaded by

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

Php limbaj de programare

Codul de programare in Php se numeste script.

<?php

. ?>

<?
echo Salut, lume;
Echo Salut, lume;
Print salut, lume;
?>
Pentru compilare este nevoie de un server local sau un hosting pentru lucrul cu php,
apache si sql. Server local compilator pentru php (de ex. Xampp sau Denver).
Task!
Info on Php Compilator Online.
Specificatoare:
/n -> rind nou la print

<?php
Echo <!doctype html>
<head>
<html><title> t </title>
</head>
<body>
Mesaj
</body>
</htmn>;
?>
Salvare: index.php
<!doctype html>
<head>
<html><title> t </title>
</head>
<body>
<p> nr este egal cu =
<?php echo$nume;?>
</body>
</html>

----- daca este asta, se salveaza deja in .php

<!doctype html>
<head>
<html><title> t </title>
</head>
<body>
Calculul sumei
<?php $a=3; $b=4;?>
Este: suma= <?php
$s=a+b; echo $s; ?>
</body>
!!! Pe ecran va aparea Calculul sumei este: suma=7

define(NUM_MAX, 100); definirea unei constante; ulterior utilizam doar numele.

Operatori:
$v=1; $v=1+2;
$v+=3; $v=$v+3;
$v-=4; $v=$v-4;
$v=$v*5; $v*=5;
$v%=2; $v=$v%2; restul impartirii la 2;
$v==$c egal
$v===$c identic
$v!=$c diferit de
$v<>$c
$a+=1; $a++
$a=3;
Print $a++; //3 (afiseaza si apoi adauga);
Print $a; //4 (afiseaza rezultatul adaugarii);
______
Print ++$a; //4 (adauga si apoi afiseaza);
Print $a; //4

Instructiuni:
If (conditie) instructiune;
<?php
$a=3; $b=4;
If ($a<$b)
Echo $b;
If (cond) instructiune;
Else instructiune;

Pentru bloc de instructiuni (mai mult de una singura) utilizam { }.

Switch case in pascal


Switch (variabila)
{case valoare1; instructiune;
break;
Case valoare; instructiune;
break;
} default instruction;

For = structuri repetitive


For (conditia initiala; conditia de lucru; schimbarea conditiei initiale)
Instructiuni
Ex. :
S=1+2+3+4+5+6+7+8+9
$s=0;
For ($i=1; $i<10; $i++)
$s=$s+$i; echo $s;
Tip: (Fara acolade va afisa doar ultimul $s=45, daca punem acolade el ia si echo ca
instructiune si vor aparea fiecare $s; )
While = repetitiv
While (conditie) {
Instructiuni;
Schimbarea conditiei initiale; }
Ex. :
$s=0;
$i=1;
While ($i<10) {
$s=$s+$i;
$i++; }
Vector tablou unidimensional
$a[10]={1,2,3} (, unde a este vectorul, 10 marimea lui)
Indicii pot fi orice, nu neaparat numere precum in Pascal
Ex.:

$a=array ( L=>1, M=>2, Mr=>3);


L
M
1
2

Mr
3

$b= array (Nr=>1, Nume=>A, Prenume=>B);


Nr
Nume
1
A

Prenume
B

$a[10]=array(1,2,3)
0
1

2
3

1
2

$b=array(2=>3, 4=>5, 6=>7)


2
4
3
5

6
7

Count calculeaza numarul de elemente dintr-un vector


Count ($a)=3
For ($i=0; $i<count($a);
$i++)
Echo $a[$i]; va afisa toate elementele din vector(tabel);
Foreach afiseaza toate elementele din variabila
Ex.:
Foreach ($b as $c)
Echo $c; va afisa 3,5,7 (doar valorile nu si cheile)
Ex. 2:
Foreach($b as $key=> $val)
Echo cheia este, $key, valoarea este, $val; {cheia este A valoarea este 3}
Sau echo $key, =>, $val; {A=>3}
Exista functii aritmetice => tutoriale php pe internet!
Functia include include un script din codul php in index-ul nostru
Ex.:
index1.php
<? Php
Echo Salut;
?>
Index.php
<?php

Include (index1.php);
?>

Isset (variabila) modularitate


$_GET[p]
$_POST

Ex.:
<table><tr>
<td> colspan = 2 > Titlu </td> </tr>
<tr> <td> meniu < ul> <li> <a href = ?p=h> Acasa </a> </li>
<li> <a href = ?p=c> contacte </a> </li>
<li> <a href= ?p=n> noutati </a> </li>
</ul>
</td>
<td> <?php if (isset ($_GET[p]))
{switch ($_GET[p]) {
case h : include (home.html); break;
case c : include (contacte.html); break;
case n : include (noutati.html); break;
} default include (home.html);
} ?>

Cream un ciclu pentru a trece toate datele dintr-un tabel cu ajutorul while:
while ($rand= mysql_fetch_assoc($citire));
{$nume=$rand[nume];
Echo $nume;
} va afisa toate datele de la coloana nume

You might also like