-
This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
-
What's up? Couldn't help but notice you're just a guest. How about registering, it only takes like 2 minutes.
Newhax
May
24
If/Else constructs are possibly the most commonly used throughout all coding languages. It is a simple method to check a code and see if it passes a certain expression, then if true, move it onto its statement.
Basic outline of an if statement:
If you have several if statements you need to declare on a variable, then the if/else construct would be used.
If expression1 is true, then statement1 will be the only one executed.
If expression1 is false and expression2 is true, then statement2 will be executed.
If neither expression1 nor expression2 are true, then...
Basic outline of an if statement:
Code:
If(expression) {
statement;
}
Code:
If(expression1) {
statement1;
} elseif(expression2) {
statement2;
} else {
statement3;
}
If expression1 is false and expression2 is true, then statement2 will be executed.
If neither expression1 nor expression2 are true, then...
May
24
Foreach statements create a way to analyze arrays by looping every element in an array separately. There are two methods used with foreach statements, one with a key value and the other without. With foreach statements, we will also introduce the use of the curly brackets { } . These are used to encase some coding within the parameters of that function, such as that of a foreach.
------------------------------------------------------------------------------
Method 1 is the simpler of the two if you are dealing with a simple array. A simple array is one that may not require the use of unique keys, or the keys may just be unimportant.
foreach($array as $value) {
Do Something;
}
There are countless amounts of uses for arrays, an example being to create a string.
Example of an echo within a foreach...
------------------------------------------------------------------------------
Method 1 is the simpler of the two if you are dealing with a simple array. A simple array is one that may not require the use of unique keys, or the keys may just be unimportant.
foreach($array as $value) {
Do Something;
}
There are countless amounts of uses for arrays, an example being to create a string.
Example of an echo within a foreach...
May
24
Now that you have learned how to create a simple key and value array, I will introduce the concept of a multidimensional array as well as some array related functions.
------------------------------------------------------------------------------
Upon creating and assigning an array to a variable, you may need to add elements to it at some point.
This can be accomplished by doing the following:
What we have done is add the element cool to the end of $array.
Output:
We can use another method if we have a specific key we wish to assign for the value:
Output:
------------------------------------------------------------------------------
Upon creating and assigning an array to a variable, you may need to add elements to it at some point.
This can be accomplished by doing the following:
Code:
<?php
$array = array('a', 'simple', 'array');
$array[] = 'cool';
print_r($array);
?>
Output:
Code:
Array
(
[0] => a
[1] => simple
[2] => array
[3] => cool
)
Code:
<?php
$array = array('a', 'simple', 'array');
$array['very'] = 'cool';
print_r($array);
?>
Code:
Array
(
[0] => a
[1] => simple
[2] =>...
May
24
Unset removes or destroys a variable or array element. Unsetting array elements is most common and a quite simple concept to comprehend.
------------------------------------------------------------------------------
Unset a simple variable:
The output would be a message describing how the variable $foo is undefined, because we have deleted it as if we had never declared it.
------------------------------------------------------------------------------
Unset an element of an array:
Output:
We deleted the 1 index from the array variable so we are just left with the 2 index....
------------------------------------------------------------------------------
Unset a simple variable:
Code:
<?php $foo = 'bar'; unset($foo); echo $foo; ?>
------------------------------------------------------------------------------
Unset an element of an array:
Code:
<?php $array = array(1 => 'foo', 2 => 'bar'); unset($array[1]); print_r($array); ?>
Code:
<?php
Array
(
[2] => bar
)
?>
May
24
There is a possibility that this might frighten you from attempting to learn PHP ever again, don't let it.
What is an array? An array is essentially a list that stores and associates multiple values at once and is done by assigning values to specific keys.
Example is a simple value array:
Output:
Each value in the original array is assigned a unique key once it is printed.
------------------------------------------------------------------------------
Example of a simple associative array:
Note how foo is the key and bar is the value, the => indicates that it is an associative...
What is an array? An array is essentially a list that stores and associates multiple values at once and is done by assigning values to specific keys.
Example is a simple value array:
Code:
<?php
$array = array('foo', 'bar');
print_r($array);
?>
Code:
Array
(
[0] => foo
[1] => bar
)
------------------------------------------------------------------------------
Example of a simple associative array:
Code:
<?php
$array = array('foo' => 'bar');
?>
Page 1 of 239
Loading...