Skip to main content

PHP Tutorial - Lesson 1: Introduction to PHP - Part III

Variables and Strings

•PHP is a Loosely Typed Language: in PHP there is no need to declare variable before giving it a value. Unlike Strongly Typed Languages such as C++ and Java that before assigning a value to a variable you have to determine the type of that variable int, float, char…etc.
• variables in php are not preceded with a type. It is the responsible of the interpreter to determine the type of the variable.
•To declare and use variable in php follow these rules:
       - each php variable should begin with $ sign followed by character or underscore.

       - php variable name might include alpha-numeric and underscores only.
       - if the variable name consists of more than one word you could use two methods for naming the variable. First method is, using underscore between each word for example $first_name. Second method is, capitalizing first character of each word after first one for example: $pageHitCounter.
•To declare a string in php we use same method to declare integers or floats or any type, as we said before it is a loosely typed language. But string when declared we can use some functions that deal with it, such as strpos(), mb_strlen(), mb_strstr().
Example

<?php
        $intValue=10;
$floatValue=9.3;
$str1 = “Hello”;
$str1Len = mb_strlen($str1); // 5
$x = strpos($str1,”el”); // $x = 1
$key = mb_strstr($str1,”llo”); // llo
$con = $str1.” World”; // $con = “Hello World”
echo $str1.” length is “.$str1Len;
echo “first occurrence of el in $str1 is $x”;
echo “first occurrence of llo in $str1 is $key”;
echo “We use dot to concatenate strings like $con”;
?>
Notes

•We use strpos and mb_strstr functions with strings to show the first occurrence of the second parameter in the first parameter strpos(source,key) or mb_strstr(source,key).
•Function strpos returns an integer value of the first occurrence of the key in the source, if the key is found in the source, or it returns FALSE if not found.
•Strings in php zero-based index, which means that first char in the string has an index of 0 and last char has an index of length-1.
•Function mb_strstr returns a string starts from the key until end of string in source, if key is found, or it returns FALSE if not found.
•Strlen function returns the length of a string.
•We use dot operator to concatenate strings.
Comments

•As a programmers we might use comments for many reasons, such as documentary reasons, illustration for what we do in our code, reminder purposes  and more.

Comments has an advantage of it is not executed and has no effects on how the program run. 
•Documentary reasons:
•Successful programmer documents every code he writes with many of useful information such as script name, programmer of the script, date of creation, date of last modification, purpose of the script and more.
•Illustrations:
• we use comments to illustrate specific steps how effect the code, later, you will notice that some algorithms are too long, and we might forget after awhile of time why we did these steps, but when you use comments that illustrates why we did something like that will be more useful.
•Reminder:
•often we don't write our codes at once, so we may write it on iterations, which will lead us sometimes to forget where we reached in our code. but when using comments we might write notes that reminds us where we reached in our application.

<?php
//Welcome message
// Programmer Anas Jaghoub
// Date of Creation 10/oct/2009
/* Purpose of this program is welcoming the user to the site */
echo “Welcome to my page”;
?>
Note:
         Like other languages php uses single line and multiple line comments. 

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

Comments

Popular posts from this blog

سلسلة تعلم برمجة تطبيقات الاندرويد - إعداد بيئة العمل - الحلقة رقم 1

في هذه الحلقة سنقوم بتجهيز بيئة العمل، وإعداد كافة الأدوات والبيئة اللازمة للبدء في برمجة تطبيقات الاندرويد، لنتمكن في الحلقات القادمة من العمل والتطوير باستخدام الأدوات التي سنقوم بإعدادها في هذه الحلقة. 1. للبدء في تطوير أي برنامج على الاندرويد لا بد من توفر حزمة التطوير الأساسية وهي Android SDK . هذه الحزمة هي حجر الأساس لعمل أي تطبيق على الاندرويد حيث تحتوي على مجموعة كبيرة من الوظائف الاساسية والبرامج التي تمكننا من التحدث مع نظام التشغيل الاندرويد، وطلب تنفيذ العمليات والحسابات إلى غيره من الوظائف بحسب طبيعة كل تطبيق ووظائفه. وتمكننا أيضا من إنشاء أجهزة افتراضية Virtual Machines تساعدنا على تجربة التطبيقات حتى بدون وجود جهاز اندرويد  2. حزمة الجافا JDK والتي تقوم بترجمة النص البرمجي لحزمة الاندرويد وكذلك النص البرمجي للتطبيق وتحويله إلى نسخة تنفيذية قابلة للعمل على أي جهاز اندرويد 3. بيئة العمل IDE وفي هذه السلسلة سنقوم باستخدام Netbeans وذلك لسهولة استخدامه وسهولة التطبيق عليه. كما يمكن أيضا استخدام بيئة العمل Eclipse . ولغايات تبسيط هذه الحلقة و...

XHTML Tutorial–Lesson 1 Part I

Preamble: This tutorial expects that you’ve the basics of HTML 4.01, and supposes that you are familiar with its syntax and purpose. In this tutorial I’ll try to introduce XHTML concept and why we should use XHTML instead of HTML, we’ll talk about differences between HTML and XHTML. We’ll talk about some of web standards that we have to follow when developing a web-based applications. What is XHTML? XHTML stands for EXtensible Hypertext Markup Language, it is almost identical to HTML 4.01, but it is a cleaner version of HTML. We might look to XHTML from a point of view that it is an HTML defined as an XML application. Above all XHTML is recommended by W3C, and though it’s compatible and supported with all web-browsers. XHTML consists of all HTML 4.01 elements combined together with the structure of XML. Why XHTML? The problem of HTML where on no standardization for web browsers, many web browsers around the world, each browser implemented in a way differs from other browsers,...

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...