Phpdynamic

PHP and MySQL Programming Guides, Tutorials, & Articles

Posts Tagged PHP

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 Constants?

MAR 25

PHP Constants

A constant represents a literal value that will remain the same, for example the value for PI (3.14), the number of days in a week (7), or the number of ounces in a pound (16) all represent numbers that will never change. A constant can store a value similar to a variable, but that value cannot be modified during the execution of a script, once that constant is initialized. Constants are created using the built-in define() function, by assigning an identifier name to the constant along with a literal value.

Read more…

What Are Variables?

MAR 18

PHP Variables

We can all probably remember variables from algebra class during our high school years, remember expressions like x + 2 = 10 or something fun like 5x(-3x3)2 ? Well, maybe some of us can, don’t worry were not going to be solving any equations here. Anyways, besides this nostalgic moment, how do variables fit into the picture?

A variable is a location in a computer’s memory, identified by a symbolic name, that can hold different values of data at different times during the execution of a program. Variables can store and retrieve data that is essential to your scripts, things like the number of visitors to your website, the total amount due at checkout, the name of the person currently logined in, or the age of the user can all be stored in variables.

Read more…

Using PHP to Generate Output

MAR 18

The echo() Statement

Without displaying any output, we probably won’t be able to accomplish anything useful with our scripts, hence PHP provides several different methods of passing output to the web browser and essentially providing the user with some feedback. In particular, we’ll cover the echo() statement and the print() statement, which are the two most common ways of displaying output to the browser.

Referring back to our “Hello World” example, you can probably guess what the echo() statement does. It simply displays or echoes output directly to the browser, in this case the string “<p>Hello World!</p>” was passed to the browser and displayed to the user.

<html>
<head>
  <title>Hello World Script</title>
</head>
<body>
<?php
  //this is some php code
  echo '<p>Hello World!</p>';
?>
</body>
</html>

Read more…

Beginning PHP – Language Syntax

MAR 11

Syntax and Semantics

All programming languages must follow a proper syntax, which is a set of rules that control the arrangement of words, numbers, and symbols for writing valid code; and PHP is no exception. In the same sense that English sentences have to follow certain grammatical rules to have a proper meaning (semantic), all PHP statements must follow a certain set of syntax rules to execute correctly.

Read more…

Beginning PHP – The Fundamentals

MAR 10

The Whole Picture

You have an operational web server with PHP installed and configured, and you might be wondering how all the pieces fall together and perform anything useful. Before we move along any further, we would like to share the underlying concept on how PHP and Apache work together; as this will provide you with a solid foundation to understanding the examples used throughout the tutorial.

Read more…

Getting Started with PHP

MAR 10

What is PHP?

PHP is a general-purpose scripting language that is widely used for web development. The official name for PHP is Hypertext Preprocessor, it is embedded directly within an HTML page and interpreted by the web server every time the page is loaded. One of PHP’s greatest advantage is its practical and simple design, allowing both beginning and experienced programmers to quickly develop dynamic content for websites and larger web applications.
Read more…