قواعد اللغة PHP

التوصيات للدورات:

تُنفذ سكربت PHP على الخادم، ثم يتم إرسال نتائج HTML النقية إلى المتصفح.

قواعد الأساس لـ PHP

سكربت PHP يمكن أن يتموضع في أي مكان في المستند. <?php البداية، ب ?> النهاية:

<?php
// هنا كود PHP
?>

امتداد الملف الافتراضي لملفات PHP هو ".php".

ملفات PHP عادة ما تحتوي على علامات HTML وبعض كود PHP.

في المثال التالي، ملف PHP بسيط يحتوي على سكربت PHP يستخدم دالة "echo" لعرض النص "Hello World!" على صفحة الويب:

Example

<!DOCTYPE html>
<html>
<body>
<h1>صفحة PHP الأولى</h1>
<?php
echo "Hello World!";
?>
</body>
</html>

Run Example

تعليقات:تعليمات PHP تنتهي بمكتبة (؛). علامة الإغلاق للكود في PHP تُظهر مكتبة تلقائيًا (لذلك لا تحتاج إلى استخدام مكتبة في السطر الأخير للكود في كتلة PHP).

تعليقات PHP

تعليقات الكود في PHP لا يتم قراءتها وتنفيذها كبرنامج. وظيفتها الوحيدة هي أن تكون للقراءة من قبل محرري الكود.

تعليقات تستخدم ل:

  • فهم الآخرين العمل الذي تقوم به - التعليقات يمكن أن تساعد المبرمجين الآخرين على فهم العمل الذي تقوم به في كل خطوة (إذا كنت تعمل في فريق)
  • Remind yourself of what you have done - most programmers have experienced returning to a project after one or two years and having to reconsider what they have done. Comments can record your thoughts when writing code.

PHP supports three types of comments:

Example

<!DOCTYPE html>
<html>
<body>
<?php
// This is a single-line comment
# This is a single-line comment
/*
This is a multiline comment block
It spans
Multiline
*/
?>
</body>
</html>

Run Example

PHP Case Sensitivity

In PHP, all user-defined functions, classes, and keywords (such as if, else, echo, etc.) are not case-sensitive.

In the following example, all three echo statements are valid (equivalent):

Example

<!DOCTYPE html>
<html>
<body>
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
</body>
</html>

Run Example

However, in PHP, all variables are case-sensitive.

In the following example, only the first statement will display the value of the $color variable (this is because $color, $COLOR, and $coLOR are considered three different variables):

Example

<!DOCTYPE html>
<html>
<body>
<?php
$color="red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
</body>
</html>

Run Example