How to sort a multi-dimensional array by value with PHP?

Sometimes, we want to sort a multi-dimensional array by value with PHP.

In this article, we’ll look at how to sort a multi-dimensional array by value with PHP.

How to sort a multi-dimensional array by value with PHP?

To sort a multi-dimensional array by value with PHP, we can use the usort function.

For instance, we write

usort($arr, function($a, $b) {
    return $a['order'] - $b['order'];
});

to call usort with the $arr array that we want to sort in place.

The 2nd argument is a function that returns a number.

If it’s positive, then we put $a before $b.

If it’s negative, then we put $b before $a.

And if it’s 0, we keep the order the way it is.

We sort by the value of the 'order entry in $a and $b in ascending order, so we subtract the values of the 'order' field in the associative array.

Conclusion

To sort a multi-dimensional array by value with PHP, we can use the usort function.

How to calculate the difference between two dates using PHP?

Sometimes, we want to calculate the difference between two dates using PHP.

In this article, we’ll look at how to calculate the difference between two dates using PHP.

How to calculate the difference between two dates using PHP?

To calculate the difference between two dates using PHP, we can use the date’s diff method.

For instance, we write

$date1 = new DateTime("2021-03-24");
$date2 = new DateTime("2022-06-26");
$interval = $date1->diff($date2);
echo $interval->y;
echo $interval->m;
echo $interval->d;

to create 2 DateTime objects $date1 and $date2.

Then we call $date->diff with $date2 to find the difference between $date2 and $date1.

We get the year difference from $interval->y , the month difference from $interval->m and the day difference from $interval->d.

We can also compare dates directly with comparison operators.

For instance, we write

$date1 = new DateTime("2021-03-24");
$date2 = new DateTime("2022-06-26");

var_dump($date1 == $date2);
var_dump($date1 < $date2);
var_dump($date1 > $date2);

to compare the 2 dates.

Conclusion

To calculate the difference between two dates using PHP, we can use the date’s diff method.

How to make a redirect in PHP?

Sometimes, we want to make a redirect in PHP.

In this article, we’ll look at how to make a redirect in PHP.

How to make a redirect in PHP?

To make a redirect in PHP, we can use the header function.

For instance, we write

header('Location: '.$newURL);

to call header with 'Location: '.$newURL to redirect to the URL stored in $newUrl.

This will send the Location response header with the new location.

The 302 status code will be used for the redirect.

Conclusion

To make a redirect in PHP, we can use the header function.

How to use bcrypt for hashing passwords in PHP?

Sometimes, we want to use bcrypt for hashing passwords in PHP.

In this article, we’ll look at how to use bcrypt for hashing passwords in PHP.

How to use bcrypt for hashing passwords in PHP?

To use bcrypt for hashing passwords in PHP., we can use the password_hash function.

For instance, we write

echo password_hash('helloworld', PASSWORD_DEFAULT)."\n";

to call password_hash with the plain password string and PASSWORD_DEFAULT to create a hash from the password with the default settings.

We can also set a few options.

For instance, we write

$options = [
  'cost' => 20
];
echo password_hash('helloworld', PASSWORD_BCRYPT, $options)."\n";

to use PASSWORD_BCRYPT to encrypt the password with bcrypt and 20 rounds.

To verify the password, we use password_verify.

For instance, we write

password_verify('helloworld', $hash)

to check that the $hash password hash has the same hash as 'helloworld'.

Conclusion

To use bcrypt for hashing passwords in PHP., we can use the password_hash function.

How to Store Objects in HTML5 localStorage with JavaScript?

Sometimes, we want to Store Objects in HTML5 localStorage in our JavaScript app.

In this article, we’ll look at how to store objects in HTML5 localStorage with JavaScript.

How to Store Objects in HTML5 localStorage with JavaScript?

To store objects in HTML5 localStorage with JavaScript, we can use JSON.stringify to convert the JavaScript object into a string.

Then we use the localStorage.setItem method to save the stringified JSON object into local storage.

For instance, we write

const testObject = {
  'one': 1,
  'two': 2,
  'three': 3
};

localStorage.setItem('testObject', JSON.stringify(testObject));

const retrievedObject = localStorage.getItem('testObject');

console.log(jSON.parse(retrievedObject));

to call JSON.stringify with testObject to return the string version of testObject.

Then we call setItem with the key of the local storage entry and the value set to the stringifyed JSON object.

We have to convert testObject to a string before storing it with setItem since local storage only store strings.

When we want to retrieve the item, we call localStorage.getItem with the 'testObject' key.

And then we call JSON.parse with the retrievedObject string to convert the string back to JavaScript object.

Conclusion

To store objects in HTML5 localStorage with JavaScript, we can use JSON.stringify to convert the JavaScript object into a string.

Then we use the localStorage.setItem method to save the stringified JSON object into local storage.

How to include a PHP variable inside a MySQL statement?

Sometimes, we want to include a PHP variable inside a MySQL statement

In this article, we’ll look at how to include a PHP variable inside a MySQL statement

How to include a PHP variable inside a MySQL statement?

To include a PHP variable inside a MySQL statement, we can create prepared statements.

For instance, we write

$first_name = "jane";
$last_name = 'smith'
$query = "INSERT INTO contents (first_name, last_name, description) 
             VALUES(?, ?, 'whatever')";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("ss", $first_name, $last_name);
$stmt->execute();

to call $mysqli->prepare with $query to create a prepared statement from the string.

The ? symbol is the placeholder for the the values

Conclusion

To iterate over an array in JavaScript, we can use the for-of loop or the JavaScript array forEach method.

We call bind_param with the values we want to replace the ? with in the $query string.

"ss" means both placeholders are strings.

Escaping is done automatically to prevent SQL injection.

Finally, we call execute to run the query.

Conclusion

To include a PHP variable inside a MySQL statement, we can create prepared statements.

How to Get a Timestamp in JavaScript?

Sometimes, we want to get a timestamp in JavaScript.

In this article, we’ll look at how to get a timestamp in JavaScript.

How to Get a Timestamp in JavaScript?

To get a timestamp in JavaScript, we can use some operators and functions.

For instance, we write

+new Date();
new Date().getTime();
Number(new Date())

to return the timestamp of the current datetime.

We use + before new Date() to get the timestamp of the current datetime in milliseconds.

Likewise, we use the getTime method to return the same thing.

Also, we can call Number with new Date() to return the current datetime’s timestamp

Conclusion

To get a timestamp in JavaScript, we can use some operators and functions.

How to access an array/object with PHP?

Sometimes, we want to access an array/object with PHP.

In this article, we’ll look at how to access an array/object with PHP.

How to access an array/object with PHP?

To access an array entry with PHP, we can use square brackets.

And to access object properties, we use the -> operator.

For instance, we write

echo $arr{0};

to print the first entry of the $arr array.

To access object properties, we use the -> operator.

For instance, we write

echo $foo->bar->property;

to print the value of the property property in the bar object that’s a property of the $foo object.

Conclusion

To access an array entry with PHP, we can use square brackets.

And to access object properties, we use the -> operator.