[ This is a content summary only. Visit our website https://ift.tt/1b4YgHQ for full links, other content, and more! ]
by Mehwish Mehmood via Digital Information World
"Mr Branding" is a blog based on RSS for everything related to website branding and website design, it collects its posts from many sites in order to facilitate the updating to the latest technology.
To suggest any source, please contact me: Taha.baba@consultant.com
Long-scrolling One Pager with a big image pop-up portfolio section for NYC-studio, 94 Photography.
This article was created in partnership with BAWMedia. Thank you for supporting the partners who make SitePoint possible.
Let's start with this scenario. You need to create a fantastic website for your client, you want to do so quickly, and your client is hoping for the same.
How about creating a website from scratch with HTML and CSS? It's an option, but it also takes too much time – not to mention effort.
Slides offers a different approach for web designers, and developers as well.
What's so different about Slides? Read on.
Slides is an advanced static website builder that is easy to use. It's unique in the sense that it's unlike any website builder you've likely used before.
Slides lets you customize your design as you're creating it. When you have the design you want, you can customize it even further if you wish.
It's simply a matter of selecting slides you want to use. Then, you can add panels, features, and settings, and download the result.
Do you have additional customizations in mind? It's simply a matter of opening the HTML file and using your favorite code editor. You can add features you or your client might want.
With Slides 4, you can create designs that fit right in with the latest trends. You can also add your own custom scripts, and incorporate a host of other improvements.
Everything starts with the Generator. It is an intuitively-designed tool that will save you hours of work. Although the Generator is easy to work with, you'll find the links to its manual.
The post How to Create Websites with Slides, a Developer-Friendly Tool appeared first on SitePoint.
In this post, you'll learn the basics of arrays in PHP. You'll learn how to create an array and how to use associative and multidimensional arrays, and you'll see lots of examples of arrays in action.
In PHP, an array is a data structure which allows you to store multiple elements in a single variable. These elements are stored as key-value pairs. In fact, you can use an array whenever there’s a need to store a list of elements. More often than not, all the items in an array have similar data types.
For an example, let’s say you want to store fruit names. Without an array, you would end up creating multiple variables to store the different fruit names. On the other hand, if you use an array to store fruit names, it might look like this:
<?php
$array_fruits = array('Apple', 'Orange', 'Watermelon', 'Mango');
?>
As you can see, we’ve used the $array_fruits variable to store the different fruit names. One great thing about this approach is that you can add more elements to the $array_fruits array variable later on.
There are plenty of ways to manipulate values in the array variable—we’ll explore these in the later part of this article.
In this section, we’ll explore how to initialize an array variable and add values in that variable.
When it comes to array initialization, there are a few different ways. In most cases, it’s the array() language construct which is used to initialize an array.
<?php $array = array(); ?>
In the above snippet, the $array variable is initialized with a blank array.
As of PHP 5.4, you can also use the following syntax to initialize an array.
<?php $array = []; ?>
Now, let’s see how to add elements to an array.
<?php $array = []; $array[] = 'One'; $array[] = 'Two'; $array[] = 'Three'; echo '<pre>'; print_r($array); ?>
The above snippet should produce the following output:
Array
(
[0] => One
[1] => Two
[2] => Three
)
The important thing to note here is that an array index starts with 0. Whenever you add a new element to an array without specifying an index, the array assigns an index automatically.
Of course, you can also create an array already initialized with values. This is the most concise way to declare an array if you already know what values it will have.
<?php $array = ['One', 'Two', 'Three']; ?>
In the previous section, we discussed how to initialize an array variable. In this section, we’ll explore a few different ways to access array elements.
The first obvious way to access array elements is to fetch them by the array key or index.
<?php $array = ['One', 'Two', 'Three']; // get the first element of the $array array echo $array[0]; echo "<br>"; // get the second element of the $array array echo $array[1]; echo "<br>"; // get the third element of the $array array echo $array[2]; echo "<br>"; ?>
The above snippet should produce the following output:
One Two Three
A cleaner way to write the code above is to use a foreach loop to iterate through the array elements.
<?php
$array = ['One', 'Two', 'Three'];
foreach ($array as $element) {
echo $element;
echo '<br>';
}
?>
The above snippet should produce the same output, and it takes much less code.
Similarly, you can also use the for loop to go through the array elements.
<?php
$array = ['One', 'Two', 'Three'];
$array_length = count($array);
for ($i = 0; $i < $array_length; ++$i) {
echo $array[$i];
echo '<br>';
}
?>
Here, we're using the for loop to go through each index in the array and then echoing the value stored in that index. In this snippet, we’ve introduced one of the most important functions you’ll end up using while working with arrays: count. It’s used to count how many elements are in an array.
In this section, we’ll discuss the different types of array you can use in PHP.
An array with the numeric index falls in the category of an indexed array. In fact, the examples we’ve discussed so far in this article are indexed arrays.
The numeric index is assigned automatically when you don’t specify it explicitly.
<?php $array = ['One', 'Two', 'Three']; ?>
In the above example, we don't specify an index for each item explicitly, so it'll be initialized with the numeric index automatically.
Of course, you can also create an indexed array by using the numeric index, as shown in the following snippet.
<?php $array = []; $array[0] = 'One'; $array[1] = 'Two'; $array[2] = 'Three'; ?>
An associate array is similar to an indexed array, but you can use string values for array keys.
Let’s see how to define an associative array.
<?php $employee = [ 'name' => 'John', 'email' => 'john@example.com', 'phone' => '1234567890', ];
Alternatively, you can use the following syntax as well.
<?php $employee = []; $employee['name'] = 'John'; $employee['email'] = 'john@example.com'; $employee['phone'] = '1234567890'; ?>
To access values of an associative array, you can use either the index or the foreach loop.
<?php
$employee = [
'name' => 'John',
'email' => 'john@example.com',
'phone' => '1234567890',
];
// get the value of employee name
echo $employee['name'];
// get all values
foreach ($employee as $key => $value) {
echo $key . ':' . $value;
echo '<br>';
}
?>
As you can see, here we got the name by querying it directly, and then we used the foreach loop to get all the key-value pairs in the array.
In the examples we’ve discussed so far, we’ve used scalar values as array elements. In fact, you can even store arrays as elements within other arrays—this is a multidimensional array.
Let’s look at an example.
<?php $employee = [ 'name' => 'John', 'email' => 'john@example.com', 'phone' => '1234567890', 'hobbies' => ['Football', 'Tennis'], 'profiles' => ['facebook' => 'johnfb', 'twitter' => 'johntw'] ]; ?>
As you can see, the hobbies key in the $employee array holds an array of hobbies. In the same way, the profiles key holds an associative array of the different profiles.
Let’s see how to access values of a multidimensional array.
<?php $employee = [ 'name' => 'John', 'email' => 'john@example.com', 'phone' => '1234567890', 'hobbies' => ['Football', 'Tennis'], 'profiles' => ['facebook' => 'johnfb', 'twitter' => 'johntw'] ]; // access hobbies echo $employee['hobbies'][0]; // Football echo $employee['hobbies'][1]; // Tennis // access profiles echo $employee['profiles']['facebook']; // johnfb echo $employee['profiles']['twitter']; // johntw ?>
As you can see, the elements of a multidimensional array can be accessed with the index or key of that element in each array part.
In this section, we'll go through a handful of useful array functions that are used frequently for array operations.
count FunctionThe count function is used to count the number of elements in an array. This is often useful if you want to iterate an array with a for loop.
<?php $array = ['One', 'Two', 'Three']; // print the number of elements in the array echo count($array); // the above code should output 3 ?>
is_array FunctionThis is one of the most useful functions for dealing with arrays. It's used to check if a variable is an array or some other data type.
<?php
$array = ['One', 'Two', 'Three'];
// check if the variable is an array
if (is_array($array))
{
// perform some array operation
}
?>
You should always use this function before you perform any array operation if you're uncertain of the data type.
in_array FunctionIf you want to check if an element exists in the array, it's the in_array function which comes to the rescue.
<?php
$array = ['One', 'Two', 'Three'];
// check if a variable is an array
if (in_array('One', $array))
{
echo 'Yes';
}
else
{
echo 'No';
}
?>
The first argument of the in_array function is an element which you want to check, and the second argument is the array itself.
explode FunctionThe explode function splits a string into multiple parts and returns it as an array. For example, let's say you have a comma-separated string and you want to split it at the commas.
<?php
$string = "One,Two,Three";
// explode a string by comma
$array = explode(",", $string);
// output should be an array
echo '<pre>';
print_r($array);
// output
/*Array
(
[0] => One
[1] => Two
[2] => Three
)*/
?>
The first argument of the explode function is a delimiter string (the string you're splitting on), and the second argument is the string itself.
implode FunctionThis is the opposite of the explode function—given an array and a glue string, the implode function can generate a string by joining all the elements of an array with a glue string between them.
<?php
$array = ['One', 'Two', 'Three'];
$string = implode(",", $array);
// output should be a string
echo $string;
// output: One,Two,Three
?>
The first argument of the implode function is a glue string, and the second argument is the array to implode.
array_push FunctionThe array_push function is used to add new elements to the end of an array.
<?php
$array = ['One', 'Two', 'Three'];
array_push($array, 'Four');
echo '<pre>';
print_r($array);
// output
/*Array
(
[0] => One
[1] => Two
[2] => Three
[3] => Four
)*/
?>
The first argument is an array, and the subsequent arguments are elements that will be added to the end of an array.
array_pop FunctionThe array_pop function removes an element from the end of an array.
<?php
$array = ['One', 'Two', 'Three'];
$element = array_pop($array);
echo '<pre>';
print_r($array);
// output
/*Array
(
[0] => One
[1] => Two
)*/
?>
The array_pop function returns the element which is removed from an array, so you can pull it into the variable. Along with array_push, this function is useful for implementing data structures like stacks.
That's all you need to get started coding with arrays in PHP. You saw how to create arrays and how to retrieve elements from them. You learned the different types of arrays in PHP, and you got a look at some of the most useful built-in PHP functions for working with arrays.