Skip to main content

Posts

Showing posts from October, 2010

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

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

PHP and HTML •           At beginning of our php tutorial, we said that php can embed any language inside it such as html, and it might work like (CGI). •           Now, we are going to work with graphical user interface (GUI). •           Hope you’ll enjoy your time while working with something great, useful, and simple.       One of the most important things, that makes user like your site, and come back again, the design of your site. It is not a secret, when I tell you that all php web applications depends completely on html and some JavaScript, that makes your site active, and finally, some CSS styles. <?php echo ‘<font style=“font-size:15px; font-family:arial; color:#000066; font-weight:bold;”>Welcome to my site</font>’; ?> •           When writing php code that embeds other scripting language, hope you follow these rules. •           When you embed non-php script with php, we deal with non-php script as a string. •           So we might store the code with a

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

Arrays What is a variable? • A variable is a storage area that holds only one value. What is an array? • A special variable that holds multiple values. • One of the most powerful properties in php is Arrays. • PHP arrays have a property of being dynamic, that it is not limited with a size for it, you can add elements to it as needed. • Arrays in php also loosely typed. • There are three kinds of arrays in php, numeric, associative, and multidimensional. Each kind has its area of usability. Numeric array: zero-based index. • You can declare numeric array in php in more than one method. Example: <?php $age= array(“30”,”21”,”19”); $age[3] = “17”; $age[]= “35”; echo $age[0].” “.$age[1].” “.$age[2].” “.$age[3].” “.$age[4]; ?> • Previous script shows you how powerful it is to an array to be dynamic, so you can expand it as needed. • You can declare array and initialize it with values like $age = array(“value1”,”value2”) • You can expand it, by two methods. if you know the

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

Loops •           As we explained before, PHP syntax is similar to C/C++ and PERL. •           Loops at PHP, Not at all, are same as any loop in C/C++. But also, has a special type of loop will be illustrated when using arrays. While Loop •           Sometimes you need a block of instructions to be repeated, as long as some condition is true. •           Here is an example <?php $x = 1; while($x<10) { echo $x.”  “; $x++; } ?> •           Previous script is a simple application, that prints numbers from 1 to 9. as a programmer, you should be familiar with this kind of loop. Do… While Loop •           In some cases you might want some instructions to be executed at least once, and be repeated as long as condition is true, best choice of loop here to be chosen is do … while loop. <?php $i=1; do { $i++; echo $i.” “; } while($i <10); ?> •           Previous script prints numbers from 2 to 9. For Loop •           Often, for loop is used when you know how many times sha

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

Server Side Includes (SSI) •           In order to organize your work, and be a professional programmer, simplify your code. There are many things you could do, to organize your code, and facilitate development acceleration to your application. •           Here I’ll put some advices to you as a programmer that might help you in your job. •           First thing, simplify, and simplify… Make it easy. Your script should do only one job. This will facilitate handling errors. •           Second thing, it is not preferred that your function has more than three parameters to finish its job, this means that you can simplify it more. •           Use Loops only when needed. •           Don’t repeat your code. •           Divide your script in directories, and group codes that complete each other in the same directory. •           Use available (OPEN SOURCE Libraries) rather than build it yourself. This will save your time, and give your application more qualities. Remember “Don’t re-invent th

PHP Tutorial - Lesson 2: Introduction to PHP - Part II

Control Structures •Control structures in php are same as many of programming languages, its syntax is most similar to c/c++ and perl. •Nevertheless, PHP has one special control structure, distinguishes it from other programming languages. •If Statement: •if(condition) statement; •If … else Statement •if(condition) statement; •else statement; •If … elseif … else statement •if(condition) statement; •elseif(condition) statement; •else statement; •If Statement used to execute some statements if condition became true; for example if($x < 3) echo “Try again”; •At this example if $x has value less than 3 then the condition is true, so Try again will be echo-ed on the web page. Otherwise it will not. •If… else Statement, used to execute some action if condition became true. Or do something else if condition is false; for example if($x <=3) echo “Try Again”; else echo “You don‟t have permission”; •At this example if $x has value less than or equal to 3 then Try Again will be echo-ed. els

PHP Tutorial - Lesson 2: Introduction to PHP - Part I

Control Structures Cookies and Sessions •Before starting control structures in php, am going to illustrate two important concepts in web applications area, Cookies and Sessions. •What is a Cookie? •a cookie is a file embedded from the server to the user's computer. Its purpose is identifying a user.  •When you deal with web applications, actually you don’t know who the user that uses your application is. Unlike traditional or real-life applications, For example, if you work in a bank, and a user asks you to exchange him money from his bank account, you have some remarks that identify this person, such as: his name, identification number, his picture, his signature, and more details that distinguish him from others. •If we want to apply this job online, we don’t know the user who deals with the application. So we make some procedures that help us distinguish one user from another. First thing is asking the user for a username, and password. Second thing is checking for compatibility

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 me