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

PHP Tutorial–Guest Book–System Definition and Requirements

In this lesson I’m going to illustrate the system definition and requirements for the Guest Book application. It is an important step in developing any application, since this step gives us an orientation about the system and its functionality, what is expected from the system to do. actually the system definition comes from keywords from customers and end users, usually try to make it clear for what they need in the application, and what they expect the system to do.  As developers it is our role to define the requirements for the system to be developed. In our example the system definition for the Guest Book is: A system that allows visitors of the site to post their comments and feedbacks about the site, with the possibility for managing comments and maintain it easily and user friendly. On the other hand, the system requirements are: a web server, since it is clear that the developed system is going to run on the Internet, so it is a web-based application not windows-based.

تعلم تطوير تطبيقات للموبايل باستخدام الأندرويد

مرحبا أصدقائي يسعدني أن أبدأ معكم سلسلة حلقات في تعلم تطوير تطبيقات للموبايل باستخدام الأندرويد ، وسأسعى جاهدا معكم في أن تكون هذه السلسلة من أوائل السلسلات في اللغة العربية لتعليم برمجة تطبيقات الأندرويد من البداية وحتى الاحتراف. وسأحاول قدر المستطاع الشرح بلغة عربية بسيطة ومفهومة. حيث ستكون غدا إن شاء الله الحلقة الأولى من تعلم تطوير تطبيقات الموبايل باستخدام الأندرويد. أنس الجاغوب Twitter: @anasjaghoub Facebook: anasjaghoub

Web Development Best Practices (PHP Best Practices)

Websites development is one of the most challenging projects ever to develop, according to the large number of components you deal with, integrate, and manage in your application. For example, most web projects Deal with databases as their data source, and I ntegrate with external services called web services and APIs. As well the application M anages its own objects and components from views to models to controllers to plugins and so on. A skilled web developer should have a great/deep knowledge in most of components used in his system, and as well should ha ve intensive focus on how each component work and how can be used, what can be done, and what cannot. On the other hand, the huge amount of requirements, components to manage, and time frame given to you to accomplish your mission, and number of people in your team, all these factors together requires from you to organize your tasks, and priorities your requirements, do deep analysis and planning in order to have a hi