البنية print‎ في PHP

من موسوعة حسوب
< PHP
اذهب إلى التنقل اذهب إلى البحث

(PHP 4, PHP 5, PHP 7)

تطبع print‎ سلسلةً نصيةً.

الوصف

int print ( string $arg )

تُخرِج print‎‎ المعامل arg.

ليست print في الواقع دالةً حقيقية إنما هي بنيةٌ من بنى اللغة (language construct)، لذلك لا حاجة لاستخدام الأقواس لتمرير الوسائط إليها.

الاختلافان الرئيسيّان مع البنية echo هما أنَّ print لا تقبل إلا معاملًا واحدًا وأنها تعيد القيمة 1 دائمًا.

المعاملات

arg

البيانات المدخلة.

القيم المعادة

تعيد 1 دائمًا.

الأمثلة

مثال 1: أمثلة على البنية print

<?php
print("Hello World");

print "print() also works without parentheses.";

print "This spans
multiple lines. The newlines will be
output as well";

print "This spans\nmultiple lines. The newlines will be\noutput as well.";

print "escaping characters is done \"Like this\".";

// يمكنك استخدام المتغيرات داخل البنية print
$foo = "foobar";
$bar = "barbaz";

print "foo is $foo"; // foo is foobar

// يمكنك أيضًا استخدام المصفوفات
$bar = array("value" => "foo");

print "this is {$bar['value']} !"; // this is foo !

// .يؤدي استخدام اقتباس أحادي إلى طباعة اسم المتغير لا قيمته
print 'foo is $foo'; // foo is $foo

 // إذا كنت لم تستخدم أحرفًا أخرى، يمكنك أن تطبع المتغيرات فقط

print $foo;          // foobar

print <<<END
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon no extra whitespace!
END;
?>

ملاحظات

نظرًا لأن print هي بنيةُ من بنى اللغة وليست دالة، فلا يمكن استدعاؤها باستخدام الدوال المتغيرة (variable functions).

انظر أيضًا

  • البنية echo: إخراج سلسلة نصية أو أكثر.
  • الدالة printf()‎: إعادة سلسلة نصية منسقة.
  • الدالة flush()‎: إخراج ما هو موجود ضمن المخزن المؤقت للنظام.

مصادر