Skip to main content

Posts

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

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

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

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

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

First Program In PHP <? php  echo “Hello World, This is My First application in PHP”; ?> Notes: -  Each PHP script should be enclosed within <? php and ?> -  <? php tells the browser that the upcoming lines are PHP script, so PHP interpreter will start its job of translating the code. -  ?> tells the browser that this is the end of the php script. Which means that the interpreter has reached end of the script. -  echo used to print anything you want on the web page. -  If we want to print constant strings like our previous example “hello world…” it should be enclosed within single/double quotes. -  Semi colon: each php statement should be ended with semi colon, which tells the interpreter that this is the end of the statement to be executed. If we don’t end statements with semi colons compilation error will occur, and no output will appear.   Run PHP Script • To run the previous script of php , and any php script we mu...