Skip to main content

PHP Tutorial - Lesson 3: Introduction to PHP - Part IV


Functions

•          Latest topic to be discussed in this lesson is functions.
•          The real power of PHP comes from its functions.
•          In PHP there are more than 700 built-in functions.
•          In previous lessons, we discussed some of built-in functions that php offers, such as function time, date, isset, unset, session_start(), mb_strstr, mb_strlen… etc.
•          Now we are going to discuss, user-defined functions, which you are as a programmer build it yourself.
•          Functions have an advantage of keeping the browser from executing your script while page loading. How? When you put your script in a function, then it will be executed only when you call the function.

How to create a function?
•          Syntax:
•          function functionName()
•          {
•          Code to be executed.
•          }

Example:
<?php
function printName($var)
{
echo “<font style=‘font-size:14px’> $var.”</font><Br>”;
}
?>


Function Returns a value:
<?php
function sum($var1,$var2)
{
return $var1 + $var2;
}
?>

How to call a function?
<?php
function welcomeMessage()
{
echo “Welcome to my application”;
}
welcomeMessage();
?>
•          If the function returns a value, then the function can be called by storing its return value in a variable, or directly echo-ing it.

Example:
<?php
function sum($v1,$v2)
{
$s = $v1 + $v2;
return $s;
}


$x = sum(1,2); // $x = 3
echo sum(2,3); // will print 5
?>

End of lesson 3, to download this lesson as PDF click here: PHP Tutorial - Lesson 3: Introduction to PHP - Part IV
Best Wishes,
Anas Jaghoub

Comments

Popular posts from this blog

AABU GTUG @ J.U.S.T (Event)

AABU GTUG @ J.U.S.T (Event) On April 26, 2011 we have headed to Jordanian University for Science and technology (J.U.S.T), to run one of our events there. The event focused on introducing the GTUGS (Google’s Technologies Users Groups) and introducing Google’s technologies to students, such as Android and App Engine, to Chrome Extensions and HTML5 demos. Attendees were about 200 attendee, in addition to Mr. Mohammad Abu Musa the manager of Genesis Company for E-learning. Their was a lot of activities during the event, beginning with introducing the AABU GTUG, and assuring for cooperate working with students to increase the awareness of students about emergent technologies, and opening new GTUG at J.U.S.T, we announced the start of this group. We have presented a lot of sessions here is the Timetable: Subject presenter Time Hello! Inas Shoman 12:00-12:05 HTML5 Mohammad Jaradat, Abd Al Rahman Shoman 12:05-12:20 Android Inas Shoman, Mohamad Mahaqbeh, Mamoon Hwamdeh 12:20-12:35 Latest...

AABU GTUG Opening Event

On February 17, 2011 We’ve headed to Al Al Bayt University, to run one of the biggest event established there, for opening the AABU GTUG (Google’s Technologies Users Group). The event focused on introducing the GTUGS (Google’s Technologies Users Groups) and introducing Google’s technologies to students, such as Android and App Engine, to Chrome Extensions and HTML5 demos. Attendees exceeded 250 attendee, in addition to Vice president of Al Al Bayt University Dr. Hashem Al Masaeed, Deanship of College of I.T at Al Al Bayt University Prof. Ismail Ababneh, and a quite number of professors and teachers at the college of I.T at AABU, in addition to Yarmuk fm and Al Ro’aya  tv. AABU GTUG as a Google’s Technologies Users Group interested in Google’s technologies and tries as possible to increase students awareness about Google’s technologies, and introduce it to them. Their was a lot of activities during the event, beginning with the key note from me Anas Jaghoub, that included introducin...

سلسلة تعلم برمجة تطبيقات الاندرويد – إنشاء أول تطبيق للاندرويد – الحلقة رقم 2

تحدثنا في الحلقة السابقة عن إعداد بيئة العمل، وأهم الأدوات والحزم اللازمة للبدء في تطوير تطبيقات لنظام التشغيل الاندرويد. سنتحدث في هذه الحلقة عن كيفية إنشاء تطبيق للاندرويد، وما هي مكونات تطبيق الاندرويد والبنية الهيكلية ﻷي تطبيق على الاندرويد. إنشاء تطبيق باستخدام بيئة العمل Netbeans : 1. اضغط على قائمة File 2. اختر New Project. 3. اختر نوع المشروع Android 4. اضغط زر Next. 5. اضبط إعدادات المشروع كالتالي: Project Name: اسم المشروع، يفضل أن يكون اسم المشروع هو نفس اسم التطبيق وذلك لتمييزه عن باقي المشاريع الأخرى Package Name: اسم حزمة المشروع، وهنا يجب اتباع قواعد تسمية الحزم في لغة Java إذ يجب أن تتكون من مقطعين على الأقل ويفصل بين كل مقطع بنقطة، ولا يوجد فراغات. Target Platform: رقم نسخة نظام التشغيل الاندرويد التي يدعمها هذا التطبيق، وهنا لا بد من اختيار أقل رقم يتوافق معه هذا التطبيق لمزيد من التفاصيل شاهد الصورة التالية: 6. اضغط على زر Finish وهكذا نكون قد أنشأنا بمساعدة بيئة العمل Netbeans أول تطبيق للأندرويد حيث تم تصميم هيكل التطبيق، واستدعاء كافة ال...