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”;
$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
Post a Comment