You are on page 1of 24

PHP Coding Standards

‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬


PHP Coding Standards

‫ا‬ / ‫إ اد‬

‫ــــ ـ ــ ــ ــ‬ ‫ـــــ ق ا ـــ‬


‫م‬٢٠١١ - ‫هـ‬١٤٣٢
‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬

‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬


‫‪PHP Coding Standards‬‬

‫ا‬ ‫إ اد ‪/‬‬

‫ا‪ #$‬ار ا"ول‬

‫ــــ ـ ــ ــ ــ‬ ‫ـــــ ق ا ـــ‬


‫‪١٤٣٢‬هـ ‪٢٠١١ -‬م‬

‫‪2‬‬
‫‪W3arabia | www.w3a.me‬‬
‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬

‫ت‬ ‫ا‬
٤ .........................................................................................
٦ .............................................................................. ! ‫ ه"ا ا‬#
٦ ......................................................................... pear ! %‫& ه‬
٦ ................................................................................. ‫قا‬
٧ ............................................................................ ! ‫(&ت ا‬
٨ .................... Indenting and Line Length - * + ‫ ل ا‬-‫و‬ ‫زا‬0‫ ا‬.١
٩ ............................................ Control Structures - 2! ‫ ا‬345 .٢
١١ ............................................. Function Calls - ‫&ء ا وال‬8 9‫ ا‬.٣
١٣ .................................... Function Definitions - ‫ا وال‬ (*:; .٤
١٥ ............................. Class Definitions - (‫&ف‬4@A‫ا =&ت )ا‬ (*:; .٥
١٥ ............................................................ Arrays - ‫&ت‬C D ‫ ا‬.٦
١٥ ........................................................ Comments - ‫ &ت‬: ‫ ا‬.٧
١٦ .......................... Including Code - %G‫&ر‬I # ‫ ا ! د‬# E; .٨
١٦ .......................................... PHP Code Tags - %5 J;‫ ا‬%5 29‫ و‬.٩
١٧ ........................................... Naming Conventions - + ‫ ا‬.١٠
١٩ ............................................... 9& K ‫ *ة‬M 5& ‫ آ‬3 8 ‫&ت‬9‫ ا &ر‬O:5

3
W3arabia | www.w3a.me
‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬

‫‪:‬‬
‫‪ W V ، P‬و‪ T4 : +V‬و‪ ، T( U +V‬و‪ :V‬ذ ‪*M # P&5‬ور أ‪ &4+ V‬و‪&= 9‬ت أ‪# ، &4 & 8‬‬ ‫أن ا‬
‫(‪ W U‬ا‪ ، T YE ZC P‬و ‪ ZC Y E( #‬ه&دي ‪ ، T‬وأ‪ UM‬أ] ا ‪ T‬إ] ا‪ P‬و ‪ \(*M ] W‬وا‪ UM‬أن‬
‫‪ W 8‬ور‪ 3 @ T 9‬ا‪ T 8 P‬و‪ ، 2 9‬و‪ T _ 3 8‬وأ@ &‪ T5‬وأ; &‪ T8‬إ ‪ ( 3‬م ا (‪ ... #‬أ & ‪:5‬‬

‫أي ‪ 9& K * (&:‬أ‪&4c‬ء آ &‪T 5‬‬ ‫` ‪( ] 2U4 #‬‬ ‫‪ # &ً E:5‬ا * ‪ # a‬و‪ &@ DI‬ا‬ ‫إن‬
‫‪e +‬‬ ‫‪* d‬ة ا ‪ D‬ر( أو & (‪*:‬ف ‪ ! &5‬د ا * ‪ .(Source code) %a‬و ;‪ 2‬إ‪ 8‬اد ه"ا ا !‬
‫ا ‪ E‬ء ‪ 3 8‬أه ا ‪ * (&:‬ا &‪* d 9‬ة ا ‪ D‬ر( )‪ (Coding Standards‬وا ‪*:‬ف ‪ 3 8‬أه‪2‬‬
‫ه"‪ W‬ا ‪.* (&:‬‬

‫‪ &: G 2 :V‬أ‪&! h5 TV‬ن أي ‪ fgM‬آ &‪ 5‬ا ‪* d‬ة ا ‪ D‬ر( )‪ . (Source code‬و‬ ‫&‬
‫‪ #‬ا *ا ‪ . i‬و !‪ Y : #‬ذ \ ‪ Y!d &5‬ا ‪& V j D‬ج إ ‪3‬‬ ‫‪ # Y K‬ا ‪* g‬ة ‪&! 0&5‬ن ‪ Y 8‬ا ‪( :‬‬
‫ا !‪ # * l‬ا ر( وا ‪.Y :‬‬

‫إ; &‪ 48 &U8‬آ &‪ 5‬ه"‪ W‬ا ‪* d‬ة‪.‬‬ ‫ا *( ا ‪a( %‬‬ ‫‪ * (&:‬آ &‪ 5‬ا ‪* d‬ة ا ‪ D‬ر( ;‬ ‫إن‬
‫! &‪ &U 5‬و‪* (&: mC‬‬ ‫‪*-‬ق ‪9& K‬‬ ‫ر ا ‪* d‬ة ‪ T5 9o5‬ا ‪&g‬ص ( ‪; 2‬‬ ‫‪ # ] C‬أن (! آ‪Y‬‬
‫*اءة‪.‬‬ ‫‪ . 4 :‬وا ‪ U‬ف ‪ #‬ه"‪ W‬ا ‪ * (&:‬ه ‪ Y:G‬ا ‪* d‬ة ا ‪ D‬ر( أآ‪5&K *l‬‬

‫ر(‪.#‬‬ ‫‪#‬ا‬ ‫(*ه& ‪ Y K #‬ا ‪( :‬‬ ‫‪&d‬ر( ا ‪; 2 ( %‬‬ ‫‪ G‬ا ‪@&I‬‬ ‫ا ‪U * (&:‬‬ ‫ه('‬
‫وأ‪& I‬ء ا‪ . YK‬آ & أ‪8 # YU+; &UV‬‬ ‫‪ 3 8 8&+; %UC‬آ &‪ 5‬ا ‪* d‬ة ا ‪ D‬ر( ‪ a5‬دة ‪&8‬‬
‫@ &‪.&U V‬‬

‫‪ 4 :‬أ‪YEC‬‬ ‫ل ‪&5‬ن ‪(*-‬‬ ‫‪p‬‬ ‫‪ * (&: #‬آ &‪ 5‬ا ‪* d‬ة ا ‪ D‬ر(‬ ‫ا‪9&9A‬‬ ‫ا‪4‬‬ ‫إن‬
‫;*( \ آ‪ً* l‬ا ‪ Y : &5‬وآ" \ ‪ :5 Y :( #‬ك ‪p V 3 8‬‬ ‫‪ #‬ا‪*IA‬ى و !‪ 2 q4 #‬ا ‪* d‬ة ‪(* 5‬‬
‫ا ‪* d‬ة‪.‬‬

‫‪4‬‬
‫‪W3arabia | www.w3a.me‬‬
‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬

‫إن ‪&M‬ء ا‪P‬‬ ‫! &‪ 5‬ا ‪* d‬ة‪ .‬و‪ 9‬ف ‪*: V‬ف ‪ %C‬ه"ا ا !‬ ‫‪9& K‬‬ ‫أآ‪(*- # *l‬‬ ‫ه‪ +‬ك‬
‫‪ (* PEAR ! * (&: 3 8‬آ &‪ 5‬ا ‪* d‬ة ا ‪ D‬ر( ! ‪ &UV‬ا‪A‬آ‪ t . :M *l‬أن ه"‪W‬‬
‫ا ‪ D‬ر ‪Yl‬‬ ‫أآ‪* u # *l‬ه&‪ .‬و;‪ * :‬ا‪&9A‬س ‪ # ( :‬ا ‪&d‬ر(‬ ‫ا ‪*: * (&:‬و‪ C‬و‬
‫ر(‪.#‬‬ ‫‪ . Zend Framework - Solar – Horde - Drupal‬و ‪ :‬ة ‪ Y K #‬ا !‪ # * l‬ا‬

‫ً ‪ . 6 789‬و م‬ ‫< ; ‪8‬‬ ‫‪ .‬آ أ‪= +‬‬ ‫‪,‬‬ ‫‪-‬‬ ‫و ‪ 23‬ا ‪ 0 1+‬إ ‪ /‬أن ه(' ا‬
‫‪ ( +8‬ا ‪ 7‬ا ? ‪.‬‬ ‫‪@/‬‬ ‫ة ‪AB C‬‬ ‫ا‬ ‫ا‪< C‬ام ‪ 6‬و‪ 6 7E8‬أ‪ +A‬ء آ‬

‫‪ t‬أ‪ %4V‬ه‪&4‬‬ ‫‪ #‬ا ‪9‬‬ ‫ا ‪ fg; %‬ه"‪ W‬ا ‪ G& 5 * (&:‬إ ‪ 3‬ا ‪(w‬‬ ‫‪ #‬ا ‪ a‬ا‪V‬‬ ‫و( ‪ 3‬ا ‪( :‬‬
‫أه & ‪ fg( & C‬آ &‪ 5‬ا ‪* d‬ة ا ‪ D‬ر( ‪&8 Y!d5‬م ‪ .‬و ‪":‬ر‪ %V‬ا &رئ ا !*(‪ 2‬إذا و‪G‬‬ ‫&و ‪x; y‬‬
‫‪ 3x‬ه ر{ ان ا‪ 3 &:; P‬أو‪ C‬و;‪ e +‬ا ‪ E‬ء‬ ‫‪ t‬أن ا‬ ‫‪ O:5‬ا *آ&آ وا ‪ f 4‬ه‪ &4‬أو ه‪&4‬ك‬
‫‪.‬‬ ‫‪A &U 5& ! PEAR‬‬ ‫‪* d‬ة ا ‪ D‬ر( و ‪ C*:‬أه‪! * (&: 2‬‬ ‫ا ‪ * (&:‬ا &‪9‬‬ ‫‪ 3 8‬أه‬

‫ل ه"ا ا‪ @0‬ار ‪ 3 8‬ا *(‬ ‫ا ‪&q Z‬ت وا]‪&+ 9‬رات‬ ‫آ & (‪ %V :+‬و(‪ %4C*d‬ا‪& 9‬ل ‪G‬‬
‫ا‪* ! 0‬و‪. majed@modernsys.net %V‬‬

‫" وا‪ P‬و ‪ %‬ا ‪" m C‬‬

‫نا‬

‫‪5‬‬
‫‪W3arabia | www.w3a.me‬‬
‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬

‫؟‬2 ‫; ه(ا ا‬
G‫& ر‬5 TG ‫ ر( " \ ه‬D ‫ *ة ا‬d ‫ ا‬5& ! PEAR ! * (&: 2‫&ول أه‬4 ( ! ‫ه"ا ا‬
‫*ف‬: V & V‫ وإ‬YD Y!d5 PEAR ! ‫ *ق‬V # &4‫ ه‬# V . php x # a * a % ‫و‬A‫ا‬
PEAR ! ‫ل‬ (w ‫ ا‬3 8 ‫ع‬Z-]&5 u*( # ‫ و‬. ‫ *ة‬d ‫ ا‬5& ‫ آ‬%C &‫&( *ه‬: 2‫ أه‬3 8 e C
http://pear.php.net % & ‫ ا‬e5‫ ا *ا‬3 8 % 9* ‫& ا‬U:K ‫ ز(&رة‬#! (

‫ ؟‬PEAR 7 ‫ه‬

PHP Extension and Application Repository ‫ &رة‬: ‫&ر‬D I‫ ا‬%‫ ه‬PEAR ‫إن آ‬
#! ( % ‫ ا‬php ‫&ت‬V ! Y 8 ‫&ر‬-‫&م ; ز( وإ‬qV ، ‫*ى‬I‫ &رة أ‬:5‫ و‬. php ‫&ت‬ ;‫ و‬m Z ‫ن‬wg
5& ‫ آ‬Y:a &U ‫ ا‬g 9‫ ا‬#! ( % ‫ وا‬، & + &U a *5 2; ‫&ت‬V ! 3 8 ‫ي‬ ;t .&U ‫ ا‬g 9‫&دة ا‬8‫إ‬
‫ر; م‬D ‫ا‬ (modules) V ‫و ات‬ ! ‫ ا‬W"‫* ه‬C ; . php %C U9 *l‫ *ة أآ‬d ‫ا‬
. 4 ‫ات ا‬ ‫ ا‬W"‫ *ة ه‬M 5& ! - *C ; & ‫ آ‬. &U V& @‫ و‬T:(‫ ز‬5

: G7E ‫ق ا‬1 I
! ‫* ه"ا ا‬dV 2; .( ‫نا‬ ) ‫قا‬ G
\‫ذ‬ K* ‫ رة ا‬D ‫ ا‬p 45 &ً V&a W*dV ‫&دة‬8‫ (*( إ‬# m (‫ و‬. e C K* ‫ ا‬T;‫ ر‬D5 %V&a Y!d5
. (‫&ر‬a ‫*اض ا‬uA‫ * ا‬x &ً V&a ! ‫ ام ه"ا ا‬g 9‫ا‬ ‫ ا‬48 ‫ ز‬a(‫ و‬. ‫ ا‬# m + ‫دون إذن‬

6
W3arabia | www.w3a.me
‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬

:2 ‫ تا‬1
& C l ‫ ر( ا‬D ‫ *ة ا‬d ‫ ا‬5& ‫ آ‬%C pear ! * (&: 2‫ أه‬3 ‫ *ق إ‬V ‫ ف‬9 % &:; P‫ذن ا‬h5
: %(

Indenting and Line Length - * + ‫ ل ا‬-‫زا و‬0‫ا‬ •


Control Structures - 2! ‫ ا‬345 •
Function Calls - ‫&ء ا وال‬8 9‫ا‬ •
Function Definitions - ‫*( ا وال‬:; •
Class Definitions - (‫&ف‬4@A‫*( ا =&ت )ا‬:; •
Arrays - ‫&ت‬C D ‫ا‬ •
Comments - ‫ &ت‬: ‫ا‬ •
Including Code - %G‫&ر‬I # ‫ ا ! د‬# E; •
PHP Code Tags - %5 J;‫ ا‬%5 29‫و‬ •
Naming Conventions - + ‫ا‬ •

‫ أن‬3 8 T 4 ‫ ا‬a(‫ و‬. &l 9& K ‫ *ة‬M 5& ‫ آ‬3 8 ‫&ت وا ر( &ت‬9‫ ا &ر‬O: 5 ‫ م‬V ‫ ف‬9 & ‫آ‬
m‫ ;*ه‬ZC %a *5 ‫ ام‬g 9‫& أي ا‬U p ‫ و‬e C j { %‫ & ه‬V‫إ‬ ! ‫ ه"ا ا‬%C ‫ *ات ا "آ رة‬d ‫ا‬
.‫&م‬8 Y!d5 ‫ *ة‬d ‫!*ة ا‬C 3 8 w‫ & رآ‬V‫ & وإ‬C* ‫ *ة‬d ‫ ا‬2UC ‫&و‬ %C \+ V

7
W3arabia | www.w3a.me
‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬

Indenting and Line Length - EM ‫ل ا‬1@‫ و‬I‫زا‬$‫ا‬ .١


‫ *ف‬A‫ د ا‬8 * + ‫ ل ا‬5 D (‫ و‬، %a * ‫ * ا‬+ ‫ ا( ا‬5 %C ‫زا ا *اغ‬0&5 D (
& C &U8& ;‫ إ‬a( % ‫&( * ا‬: ‫ أ & ا‬.‫ ا *وف‬# 5 % ‫&ت ا‬u‫& ا *ا‬U C & 5 %a *5 * 9 Y!
: % & ‫ آ‬%‫ * ه‬+ ‫ ل ا‬-‫زا و‬0‫ ا‬fg(
. Tap ‫ر‬w ‫ ام ا‬g 9‫ ون ا‬5 (‫&ت‬u‫*ا‬C) ‫ أ *ف‬5‫ ار أر‬5 ‫زا‬0‫ أن ;! ن ا‬a( -

.‫ *ف‬٨٥-٧٥ # 5 %a * ‫ * ا‬+ ‫ ل ا‬- ‫ أن (! ن‬a( -


: * + ‫ ل ا‬- ‫&ر‬D I] ‫&ل‬l
‫ *ف‬١٢٠ # *l‫ أآ‬%a * ‫ * ا‬+ ‫ ا‬W"‫ ل ه‬-

list($name, $age, $gender, $identity) = array(get_values($name), get_values($age),


get_values($gender), get_values($identity‫((؛‬

: % & ‫ ا‬Y!d &5 W‫&ر‬D I‫ ا‬#! (

list($name, $age, $gender, $identity) = array(


get_values($name),
get_values($age),
get_values($gender),
get_values($identity)
);

‫ أ *ف‬5‫ ار أر‬5 YI‫ ا ا‬3 ‫& إ‬U ‫إزا‬ * 9‫ ة ا‬8 3 8 C D ‫&@* ا‬48 { 5 &4 K t
. ‫*اءة‬ 5&K *l‫ *ة أآ‬d ‫ ا‬# Y:a( ‫ وه"ا‬. ‫ ون إزا‬5 Y + * 9 %C C D ‫&( ا‬UV ‫ س‬K‫و‬

8
W3arabia | www.w3a.me
‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬

Control Structures - P ‫ ا‬/+ .٢


.2! ‫ ا‬345 # &‫ *ه‬u‫ و‬if, for, while, switch # Y‫ آ‬Y d;‫و‬

: 2U4 5 ‫ ا‬:; *l‫آ‬A‫& ا‬UVA if ‫ &رة‬: ‫&ل‬l ‫وه"ا‬

if ((condition1) || (condition2)) {
action1;
} elseif ((condition3) && (condition4)) {
action2;
} else {
defaultaction;
}

‫ س‬K‫ و‬2! ‫ &رة ا‬8 # 5 YD ( (e C ‫ ار *ف وا‬5) ‫*اغ وا‬C ‫&ك‬4‫أن (! ن ه‬ a( -


‫ &ت‬Z8 # 5 ‫*اغ وا‬C \ "‫&ء ا وال وآ‬8 9‫ ا‬# 5‫& و‬U45 m(* \ ‫ وذ‬2! ‫ا‬ G (‫ ا‬5
: ‫&ل‬l . ‫*وط‬d ‫ وا‬V‫ا &ر‬

if (($age == 18) && ($gender == 'MALE')) {


//... code goes here
}

‫& أو‬U48 ‫&ء‬4x 9]‫ ا‬#! ( % ‫ ا &]ت ا‬%C 3 { } G*: ‫ اس ا‬KA‫ ام ا‬g 9‫ دا` & ا‬YE ( -
‫وث‬ # Y (‫ *ة و‬d ‫*اءة ا‬K 5&K # (w( &U ‫ ا‬g 9&C . ‫ &ري‬I‫& ا‬U ‫ ا‬g 9‫ (! ن ا‬% ‫ا‬
.2! ‫ا‬ a * 9 C&{‫ إ‬48 4 ‫ &ء ا‬IA‫ا‬

: ‫&ل‬l . : ‫ ا( ا‬5 ‫ س‬K :5 * + ‫ ا‬%U 4(‫ه& و‬ * 9 %C 2! ‫ &رة ا‬8 !; -

if ((condition1) || (condition2) ) { // ‫ا‬

(&UV ‫ س‬K 5& ‫ آ‬a(‫ و‬. ‫ أ *ف‬5‫ ار أر‬5 { } : ‫ اس ا‬K‫ أ‬YI‫ *ة دا‬d ‫ا‬ ‫ إزا‬a( -
: ‫&ل‬l .Y + * 9 %C : ‫ا‬

9
W3arabia | www.w3a.me
‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬

if ((condition1) || (condition2)) {
//... ‫ف‬ ‫أ‬ ‫ار أر‬ ‫ة‬ ‫ا‬ ‫إزا‬
}

OR ‫ و‬AND &U;& ‫ آ‬# ] 5 || ‫ &ت && و‬Z: ‫ م ا‬g +; ‫ أن‬a( -

: switch 3 8 ‫&ل‬l

switch (condition) {
case 1:
action1;
break;

case 2:
action2;
break;

default:
defaultaction;
break;
}

10
W3arabia | www.w3a.me
‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬

Function Calls - ‫ء ا وال‬ ‫ا‬ .٣


: ‫&ل‬l . (‫ س ا ا‬K‫ ا ا و‬29‫ أ‬# 5 ‫*اغ‬C ‫ ون‬5 ‫&ء ا ا‬8 9‫ا‬ a( -

get_values()

‫أن (! ن‬ a(‫ و‬e C ‫ *اغ وا‬5 &U4 5 YD ‫ا‬ a( ‫&را *ات ( ا ا‬5 ) ‫ &ت‬: 5& ‫ آ‬48 -
: ‫&ل‬l .&U K p ‫ ا &@ و‬:5 ‫ه"ا ا *اغ‬

($name, $age, $gender)

3 : *I_ # 5 \ "‫ ا( ا ا وآ‬5 ‫ س‬K # 5‫ و‬3 : ‫ أول‬# 5 ‫*اغ‬C ‫&ك‬4‫ أن (! ن ه‬a( ] -


: &l ‫&ل ا‬l . ‫&( ا ا‬UV ‫ س‬K‫و‬

get_values($name, $age, $gender);

. return : ‫ ام ا‬g 9&5 K ‫ ا ا‬:; ‫ أن‬a( -


\ ‫ وذ‬Y( - 29‫* ذو ا‬I_‫ * و‬DK 29‫ * ذو ا‬x ‫&ك‬4‫ & (! ن ه‬48 ‫&واة‬+ ‫ا‬ Z8 ‫ إزا‬-
: ‫&ل‬l . ‫*اءة ا ! د‬K 5&K ‫(&دة‬w

$short = foo($bar);
$long_variable = foo($baz);

11
W3arabia | www.w3a.me
‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬

: ‫&ل‬l .‫ ا‬G Y( - ‫ *ات‬x ‫ا‬ ‫&واة إذا آ&ن أ‬+ ‫ا‬ Z8 ‫ ة إزا‬8&K *+‫ آ‬#! (‫ و‬-

$short = foo($bar);
$thisVariableNameIsVeeeeeeeeeeryLong = foo($baz);

%C & ‫ &ت ا ا آ‬: ‫ إزا‬Y 8 #! ( ‫&ء ا وال‬8 9‫ ا‬48 ‫ *ة‬d ‫*اءة ا‬K 5&K ‫(&دة‬w ‫ و‬-
: % & ‫&ل ا‬l ‫ا‬

$this->callSomeFunction('param1', 'second', true);


$this->callSomeFunction('parameter2', 'third', false);
$this->callSomeFunction('3', 'verrrrrrylong', true);

&@ DI‫ *ف و‬٨٥ - ٧٥ # 5 ‫ا‬ %a * ‫ * ا‬+ ‫ ل ا‬- ‫& أن (! ن‬V& ‫ أ‬Y + ‫ا‬# -
: % ( & ‫ * آ‬9 # *l‫ أآ‬%C ‫ &ت‬: ‫ و{ ا‬#! ( \ " ‫ا‬ ‫ *ة‬l‫ &ت آ‬: ‫ د‬G‫ & و‬%C

$this->someObject->subObject->callThisFunctionWithALongName(
$parameterOne, $parameterTwo,
$aVeryLongParameterThree
);

‫&وزت‬a; ‫ * إذا‬9‫ ة ا‬8 3 8 ‫&واة‬+ ‫ا‬ Z8 2 + ; #! ( : ( = ) ‫&واة‬+ ‫ا‬ Z8 -


%C ‫&واة‬+ ‫ا‬ Z8 ‫أن ;! ن‬ a(‫ و‬%a * ‫ * ا‬+ ‫ل ا‬ T5 ‫ ح‬+ ‫ا‬ ‫ت ا‬Z &: ‫ا‬
: ‫&ل‬l .‫&ت‬u‫*ا‬C 5‫ ار أر‬5 ‫إزا‬ %& ‫ * ا‬+‫ا‬

$GLOBALS['TSFE']->additionalHeaderData[$this->strApplicationName]
= $this->xajax->getJavascript(t3lib_extMgm::siteRelPath('nr_xajax'));

12
W3arabia | www.w3a.me
‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬

Function Definitions - ‫ ا وال‬S 8 .٤


‫ *ة‬d ‫ا‬ ‫إزا‬ a(‫ و‬W * 9 %C ‫&( ا ا‬UV ‫ س‬K‫ ا( ا ا و‬5 ‫ س‬K ‫أن (! ن‬ a( -
: ‫&ل‬l .‫&ت‬u‫*ا‬C 5‫ار أر‬ 5 ‫ اس ا ا‬K‫ أ‬YI‫ ا‬5

function fooFunction()
{
//... code goes here
}

. return : ‫ ام ا‬g 9&5 K ‫ ا ا‬:; ‫ أن‬a( -


: ‫&ل‬l .‫ &ت‬: ‫ا‬ `&K *I_ %C ‫ *ا{ ;! ن‬C‫ ا‬2 K &U ‫ ;! ن‬% ‫ &ت )ا &را *ات( ا‬: ‫ ا‬-

($arg1, $arg2 = '')

: 9& K ‫&ل ا‬l -

function fooFunction($arg1, $arg2 = '')


{
if (condition) {
statement;
}
return $val;
}

: ‫*ى‬I‫ أ‬9& K ‫&ل ا‬l -

function connect(&$dsn, $persistent = false)


{
if (is_array($dsn)) {
$dsninfo = &$dsn;
} else {
$dsninfo = DB::parseDSN($dsn);
}

13
W3arabia | www.w3a.me
‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬

if (!$dsninfo || !$dsninfo['phptype']) {
return $this->raiseError();
}

return true;
}

%a * ‫ * ا‬+ ‫ ا‬%C T5 ‫ ح‬+ ‫ا‬ ‫&وزت ا ا ا‬a;‫ا و‬ ‫ *ة‬l‫ &ت آ‬: ‫ د‬G‫ & و‬%C -
* + ‫ ا‬p V %C ‫ &ت‬: ‫ ا‬O:5 {‫ و‬#! ( t .* 9 # *l‫ أآ‬%C ‫ &ت‬: ‫ و{ ا‬#! (
‫ ار‬5 ‫ إزا‬Y 8 & ‫ * ا‬9A‫ ا‬%C %K& ‫ وا‬C&‫آ‬ &+ ‫&ك‬4‫ ا ا إذا آ&ن ه‬29‫ا‬
( G * 9 %C ‫ ا( ا ا‬5 ‫ س‬K‫ &ت ا ا و‬: (&UV ‫ س‬K {‫و‬ a( & ‫ آ‬.‫&ت‬u‫*ا‬C 5‫أر‬

: ‫&ل‬l
function someFunctionWithAVeryLongName($firstParameter = 'something', $secondParameter = 'booooo',
$third = null, $fourthParameter = false, $fifthParameter = 123.12,
$sixthParam = true
) {
//... code goes here

14
W3arabia | www.w3a.me
‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬

Class Definitions - (‫ ف‬+#"‫ ت )ا‬U ‫ ا‬S 8 .٥


‫ *ة‬d ‫ا‬ ‫ إزا‬a(‫ و‬W * 9 %C 4D ‫&( ا‬UV ‫ س‬K‫و‬ 4D ‫ ا( ا‬5 ‫ س‬K ‫أن (! ن‬ a( -
: ‫&ل‬l .‫&ت‬u‫*ا‬C 5‫ ار أر‬5 4D ‫ اس ا‬K‫ أ‬YI‫ ا‬5

class Foo_Bar
{

//... code goes here

Arrays – ‫& ت‬1 ‫ا‬ .٦


* :( ‫ *ة وه"ا‬IA‫ا‬ ‫ ا‬:5 3 @& ‫ ا‬C&{‫ إ‬2 ( * 9‫ ة ا‬8 3 8 C D (*:; 48 -
: ‫&ل‬l . (valid PHP syntax) %5 J;‫ ا‬%5 x %C @ G

$some_array = array(
'foo' => 'bar',
'spam' => 'ham',
);

Comments – ‫ت‬ ‫ا‬ .٧


: ‫&ل‬l .* 9 # *l‫ أآ‬3 8 ‫ي‬ ( m :; 5& ! a4 ‫ا‬ `& ‫ ا‬-*d ‫ ام ا‬g 9‫ا‬ a( -

/**
* More than one lines
* !‫ أآ‬#$% ‫ * )('ي‬$+,
* ....
*/

15
W3arabia | www.w3a.me
‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬

. #w * ‫ ام ا‬g 9‫ م ا‬8‫و‬ ‫ * وا‬9 # m :; 5& ! ‫ ا &` ا !*رة‬-*d ‫ ام ا‬g 9‫ا‬ a( -


: ‫&ل‬l

// single Line

// ‫وا‬ . * $+,

Including Code – ‫ \ ر‬S ; ‫د‬1 ‫] ; ا‬8 .٨


: ‫&ل‬l . include_once : ‫ م ا‬g 9‫ ا‬4 : ‫*وط‬d5 %G‫&ر‬I # ‫ آ د‬# E; 48 -

$db_type = get_db_type();
if ($db_type == 'mysql') {
include_once('mysql.php');
}

. require_once : ‫ م ا‬g 9‫*وط ا‬M ‫ ون أي‬5 %G‫&ر‬I # ‫ آ د‬# E; 48 -


:‫&ل‬l

require_once('header.php');

PHP Code Tags – _8‫ا‬ P ‫و‬ .٩


<? ?> * D ‫ ا‬29 ‫ ا‬p ‫?< و‬php ?> ( ‫( ا‬e 4 ‫ )ا‬29 ‫ ام ا‬g 9‫ا‬ a( -

16
W3arabia | www.w3a.me
‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬

Naming Conventions – M ‫ ا‬.١٠


(classes) ‫&ف‬4@A‫&رج ا‬I ‫ ;! ن‬% ‫ *ات وا وال ا‬x ‫ا‬ +; g; TV‫ أ‬3 ‫ إ‬T 4 ‫ ا‬a(
.&U I‫دا‬ + ‫ ا‬#8 Z K

. 9 -*d5 * x ‫ ا‬29‫ ا‬%C ‫ ا ! &ت‬# 5 YD (‫ و‬W* x@ ‫ *ف‬o5 ‫ *ات‬x ‫ا‬ +; 2 ( -

$db_type = 'mysql';

. 9 -*d5 y5&l ‫ ا‬29‫ ا‬%C ‫ ا ! &ت‬# 5 YD (‫ *ف آ *ة و‬o5 y5‫ ا‬l ‫ا‬ +; 2 ( -

define ('SITE_PATH', $sitepath);

. 9 -*d5 ‫ ا ا‬29‫ ا‬%C ‫ ا ! &ت‬# 5 YD (‫ *ة و‬x@ ‫ *ف‬o5 ‫ا وال‬ +; 2 ( -

get_db_type();

# 5 YD (‫و‬ 4D ‫ ا‬29‫ أ‬# ‫ آ‬Y‫ ا( آ‬5 %C * ‫ *ف آ‬5 (classes) ‫&ف‬4@A‫ا‬ +; 2 ( -


29]‫&ر ا‬D I‫ ا‬4a;‫ و‬. @‫ د] و‬29Z ‫ أن (! ن‬a( & ‫ آ‬. 9 -*d5 %C ‫ا ! &ت‬
: ‫&ل‬l . ‫ !&ن‬0‫ ر ا‬K

HTML_Upload_Error
HTML_Template
Log

‫ ا‬8& 29‫ أ‬# ‫ آ‬Y‫ ا( آ‬5 %C * ‫ *ف آ‬5 4D ‫ ا‬YI‫ *ات دا‬x ‫ا وال وا‬ +; 2 ( -
: ‫&ل‬l % ‫و‬A‫ا‬ !‫ا‬

17
W3arabia | www.w3a.me
‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬

// ‫ات‬01( ‫ ء‬3‫أ‬
$counter
$clearCache

// ‫ ء دوال‬3‫أ‬
getData()
connect()
buildSomeWidget()

: ‫&ل‬l . 4D ‫ ا‬YI‫ دا‬%C (Private) @&g ‫ ا وال ا‬29‫ ا‬Y K 9 -*M {‫و‬ a( -

_sort()
_initTree()

18
W3arabia | www.w3a.me
‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬

: -‫ة‬ a ‫آ‬/ ‫ر ت‬ ‫ا‬b

: % & ‫ ا ! د ا‬3 ‫& إ‬V*qV

$name='Majed';
$country='Saudi Arabia';
$city='Madinah';
$site='www.yamamah.org';
$email='mr_amri@hotmail.com'
$data='My name is '.$name.' and I am from '.$country.' - '.$city.
$contact='You can contact me through my site '.$site.' or my email '.$email;
if($name=='Majed'){ echo $data.'<br />'.$contact;}

: % ( & ‫*اءة آ‬ 5&K *l‫ أآ‬j D YEC‫ رة أ‬D5 T 5& ‫ آ‬#! (

$name = 'Majed';
$country = 'Saudi Arabia';
$city = 'Madinah';
$site = 'www.yamamah.org';
$email = 'mr_amri@hotmail.com'
$data = 'My name is '.$name.' and I am from '.$country.' - '.$city;
$contact = 'You can contact me through my site '.$site.' or my email '.$email;

if ($name == 'Majed') {
echo $data.'<br />'.$contact;
}

‫*ى ذو‬I‫ * وأ‬DK 29‫ *ات ذو ا‬x ‫ د‬G \ ‫ *ات وذ‬x ‫ ا‬# 5 ‫&واة‬+ ‫ا‬ Z8 ‫زا‬h5 &4 K t
(‫ ا‬5 ‫ س‬K‫ و‬2! ‫ &رة ا‬8 # 5 YD ( (e C ‫ ار *ف وا‬5) ‫*اغ وا‬C &4 {‫ وآ" \ أ‬.Y( - 29‫ا‬
5‫ ار أر‬5 ( {} G*: ‫ اس ا‬KA‫ ) ا‬if : ‫ اس ا‬K‫ أ‬YI‫ *ة دا‬d ‫ا‬ ‫ إزا‬2; &E(‫ وأ‬. if G
.‫أ *ف‬

19
W3arabia | www.w3a.me
‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬

:% & ‫&ل ا‬l ‫ ا‬3 ‫& إ‬V*qV ‫و‬

if ($foo) {
$bar = 1;
}
if ($spam) {
$ham = 1;
}
if ($pinky) {
$brain = 1;
}

: % ( & ‫ آ‬if Y G # G Y‫ آ‬# 5 ‫&رغ‬C * 9 &4 {‫ ا *اءة إذا أ‬%C YU9‫(! ن أ‬

if ($foo) {
$bar = 1;
}

if ($spam) {
$ham = 1;
}

if ($pinky) {
$brain = 1;
}

* 9‫ ة ا‬8 3 8 & ‫ ا‬if ‫ &رة‬8 2 + ; &V‫أرد‬

if ($condition1 || $condition2 && $condition3 && $condition4) {


//... code goes here
}

: % & ‫ ا‬Y!d &5 &U 5& ‫&دة آ‬8‫ إ‬#! (

if ( $condition1
|| $condition2
&& $condition3
&& $condition4
) {
//... code goes here
}

20
W3arabia | www.w3a.me
‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬

.* 9‫ ة ا‬: %a * ‫ * ا‬+ ‫ ا‬2 + ; 3 ‫ إ‬G& ‫&ك‬4‫ & ] (! ن ه‬48 ‫ه‬ &5 ‫ ا &]ت‬YEC‫وأ‬
# ‫ * (! ن‬9‫ ة ا‬8 3 ‫& إ‬U + ; a(‫ و‬% !( & 5 ( - -*d ‫ا‬ a ‫ & ;! ن ا‬48
W"‫ ه‬V‫ *ات و &ر‬x ‫*وط آ‬d ‫ ا‬#8 * : ‫\ ا‬4! ( ‫ ا &]ت‬W"‫ ه‬Yl %C‫ و‬.&U + ; YECA‫ا‬
j { ‫ و‬.*x@‫&ت ا‬8 a 3 ‫*ط إ‬d ‫ ا‬2 + ; #! ( (* ‫ ا‬W"U5 if -*d ‫ &رة ا‬: ‫ ا‬%C ‫ *ات‬x ‫ا‬
: % & ‫ ا‬Y!d &5 5&+ ‫ *ة ا‬d ‫ ا‬5& ‫&دة آ‬8‫ إ‬#! ( * ‫ اآ‬Y!d5 \ ‫ذ‬

$is_foo = ($condition1 || $condition2);


$is_bar = ($condition3 && $condtion4);

if ($is_foo && $is_bar) {


//... code goes here
}

#! ( ‫ ( إذا‬Ternary operators ) %cZl ‫*ط ا‬d ‫ ا‬Y &: ‫ ة‬8& ‫ ا‬W"‫ ه‬p V ‫ ام‬g 9‫ ا‬#! ( & ‫آ‬
: ‫&ل‬l . * + ‫ ا( ا‬5 %C # 4 ‫&م وا‬U 9]‫ا‬ Z8 3 8 ‫&ظ‬ ‫ا‬ * 9‫ ة ا‬8 3 ‫ إ‬2 + ‫ا‬

// ‫ ا‬6 7 . 8( 9 ‫ا< ( ; م وا‬ =%


$a = $condition1 && $condition2
? $foo : $bar;

// > ( . 8( 9 ‫وا‬ . ‫ا< ( ; م‬ =%


$b = $condition3 && $condition4
? $foo_man_this_is_too_long_what_should_i_do
: $bar;

21
W3arabia | www.w3a.me
‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬

%C e +5 ‫*ط‬M m ; 48 ‫!*ا‬ K ‫&دة‬8‫إ‬ ! ‫ ا‬# &U;& : ‫وال و‬ ‫ا *اءة‬ 5&K 3 8 ‫&ظ‬


: % & ‫&ل ا‬l ‫ ا‬3 ‫& إ‬V*qV . ‫ ا( ا ا‬5

function foo($bar, $baz)


{
// ‫ط‬ ‫ @* ا‬9%
if ($foo) {
// ‫ض‬B
// ‫أن‬
// ‫'ن‬D
// 9‫ه‬
// ‫ة‬ F
// G H
// > +(,
//
// ‫ت‬ +
// ‫ا ا‬
return $calculated_value;
} else {
return null;
}
}

: % ( & ‫*ط آ‬d ‫ ا‬m ( 2 ‫ !*ًا إذا‬null ‫&دة ا‬8‫ إ‬YECA‫ ا‬#

function foo($bar, $baz)


{
// ‫ط‬ ‫ م @* ا‬% 9%
if (!$foo) {
return null;
}

// ‫ط‬ ‫ @* ا‬9%
if ($foo) {
// ‫ض‬B
// ‫أن‬
// ‫'ن‬D
// 9‫ه‬
// ‫ة‬ F
// G H
// > +(,
//
// ‫ت‬ +
// ‫ا ا‬
return $calculated_value;
}
}

22
W3arabia | www.w3a.me
‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬

‫ا‪8c‬‬
‫&رآ ً& آ & ( ‪Za5 m‬ل و‪ TUG‬و‪2 q8‬‬ ‫ا آ‪ً* l‬ا ‪&ً -‬‬ ‫ا‪ TV& 9 P‬و;‪% &:‬‬ ‫‪ %C‬ا ‪& g‬م ا‬
‫{ ع ‪&C ،‬ن أ@ ‪ # C y‬ا‪ P‬و ‪ ، W‬وان أ‪o I‬ت‬ ‫‪ ، TV& 9‬أن و‪ 5& ! %4 C‬ه"‪ W‬ا‪ #8 * 9A‬ه"ا ا‬
‫! &‪ 5‬ا ‪* d‬ة‬ ‫ل ا ‪ * (&:‬ا &‪9‬‬ ‫‪ %+ V # C‬و ‪ #‬ا ‪& d‬ن ‪ .‬وأ; ‪ 34‬أن أآ ن ‪ & y K‬ه‬
‫ا ‪ D‬ر( ‪ .‬و‪& %C‬ل و‪ G‬د ‪&q Z‬ت أو أ‪& I‬ء ‪ %C‬ه"ا ا‪ @0‬ار (‪ %V :+‬ا‪ 3 8 &U & 9‬ا *(‬
‫ا‪* ! 0‬و‪. majed@modernsys.net %V‬‬

‫& ; ‪2 :; # T‬‬ ‫‪*c‬ي ا ‪ 5*:‬ا ‪ * x‬ر‪5‬‬ ‫‪ +9‬د‪5‬‬ ‫آ & أ; م ‪ Y(wa5‬ا ‪ *!d‬وا ‪&C*:‬ن‬
‫ودروس‬ ‫ا{‬ ‫‪ g +‬م ا ‪ ، %5*:‬و & ;‪# W*d4‬‬ ‫&ت ا (‬ ‫‪&x %V&a‬ت ‪ a *5‬و;‪; 2 D‬‬
‫ى ا ‪.%5*:‬‬ ‫‪*c0‬اء ا‬ ‫‪K‬‬

‫و‪ T _ 3 8‬و@ ‪T‬‬ ‫‪ P‬رب ا ‪ ، # &:‬و@ ‪ 3‬ا ‪&4 V 3 8 2U‬‬ ‫و_‪ *I‬د‪ 8‬ا‪ &V‬أن ا‬
‫أ‪# : G‬‬

‫‪23‬‬
‫‪W3arabia | www.w3a.me‬‬
‫ر‬ ‫ةا‬ ‫ا‬ ‫ا‬ ‫ا‬

G ‫ا‬ ‫ا‬

• Pear Coding Standards.


• zend framework wiki - PHP Coding Standard (draft).

24
W3arabia | www.w3a.me

You might also like