Phpdynamic

PHP and MySQL Programming Guides, Tutorials, & Articles

Posts Tagged Intermediate

PHP Control Structures

MAR 31

Repeating Statements with Loops

Throughout our daily routines we repeat certain actions over and over again to accomplish a task. A very basic example would be the steps we take when brushing our teeth (brush, rinse, gargle) and repeat these steps until we are satisfied with the results. We can apply this same analogy to a loop construct, which is used to execute a statement or block of statements repeatedly until a condition is satisfied. Since computers are very good at repeating operations, we can use loops to perform repetitive tasks that would otherwise be very time consuming and boring.

Similar to other programming languages PHP offers two types of loops: conditional loops, which are repeated an indefinite number of times until a condition is met and iterated or counter loops, which are repeated a fixed number of times.

Read more…

PHP Conditional Statements

MAR 31

Implementing Logic in PHP

The examples we’ve looked at so far have been pretty straight-forward and not very intelligent. The code was executed without any built-in logic, that’s all fun and games, but to create scripts that can respond to different situations and alter the normal flow of control in response to those situations, we need to utilize conditional statements.

Read more…

PHP Operators

MAR 25

Operators and Operands

Up to this point we have been working with only a handful of operators such as the assignment operator and some basic arithmetic operators. PHP offers many other kinds of operators, we’ll cover and explain them in this tutorial.

An operator is a symbol that is used to perform a particular action on a operand, which is the input data used in the operation. For example, in the following expression: 2 + 3, the (+) symbol represents an operator and the numerical values 2 and 3 represent operands. PHP supports three types of operators, unary, binary, and ternary which operate on single operands, two operands, and three operands, respectively.

To hit the ground running, below is a table containing a complete listing of all operators available in PHP, with the highest-precedence operators at the top along with their associativity.

Operators Associativity
() N/A
new N/A
[] Right
! ~ ++ — (int) (double) (string) (array) (object) @ Right
/ * % Left
+ – . Left
< < >> left
< <= > >= N/A
== != === !== N/A
& Left
^ Left
| Left
&& Left
|| Left
? : Left
= += -= *= /= .= %= &= |= ^= ~= < <= >>= Left
Print Right
AND Left
XOR Left
OR Left
, Left

Read more…

Working With Data

MAR 25

PHP Data Types

A typical PHP script works on various types of data such as strings, integers, and Booleans. This classification is used to represent how that data can be manipulated. PHP has native support for both scalar data-types such as integers, strings, and Boolean and compound data-types like arrays and objects. PHP also supports two special data-types called resource and null, we’ve mentioned them here for the sake of completeness, but we’ll cover these data-types along with the array and object types in there respective tutorials.

Read more…

What are Arrays?

NOV 30

Introduction to Arrays in PHP

In previous tutorials we explained the concept of variables, which are named memory locations used to store a single value or piece of data. In a way arrays are similar to variables, but instead of just storing an individual piece of data, they can store groups of related data. A list of e-mail addresses, names of employees, or a list of products, are just some examples of data that can be stored in an array. Essentially any data that can be grouped together can be stored in an array. Once your understand the concept of arrays, it will become impossible to program without them.

An array can consist of any number of elements, or in other words data, were each element is distinguished and referenced by a unique identifier called a key. Array elements by default have numerical keys which are indexed sequentially, but they can also be indexed by user defined keys. An array that is indexed by numbers is called a numerically indexed array while an array that is indexed by strings is called an associative index array.

Numerically Indexed Arrays

In a numerically indexed array each element is indexed sequentially by an integer starting from zero by default, were the key or index relates to the element’s position within the array. Below is a simple illustration of an array. Note, the resemblance between the arrangement of an array and a table. The first column is the unique index of the array element and the second column is the actual data value. Each subsequent row follows the same pattern.

array table

Read more…