PHP 8 Nullsafe Operator

26 Jul 2022 / Jay Griggs

Pretty soon PHP8 will be mandatory all over the web.
Today we'll look at the Nullsafe operator to reduce a few lines of code.

Instead of null check conditions, you can now use a chain of calls with the new nullsafe operator.
When the evaluation of one element in the chain fails,
the execution of the entire chain aborts and the entire chain evaluates to null.

//PHP7

$country = null;

if ($session !== NULL){
   $user = $session->user;

   if($user !== NULL){
       $address = $user->getAddress();
    }

    if($address !== NULL){
       $country = $address->country;
    }

}

//PHP8

$country = $session?->user?->getAddress()?->country;

Much cleaner don't you think?

Now get coding!

Copyright… More like Copywrong

10 Jun 2022 / Jay Griggs

Copyright… More like Copywrong

Everyday I see outdated copyrights on websites... Everyday.

As developers can we do better?
I think we can.
Let's harness PHP to write it once and never be out of date again.
So of course this would go (In WordPress) in your footer.php file that sits in your themes directory.
Depending on where you are in your template you may or may not need your php start and end tags.

// set the default timezone to use.
date_default_timezone_set('UTC');
//set variable $today to todays year
$today = date("Y");
//echo it all out to the browser
echo "©" . $today;

Finally a proper date on your website.... Forever!

Now get coding!

Dont Pee In My WordPress

10 Apr 2022 / Jay Griggs

Don't Pee In My WordPress

You get a brand new WordPress site installed. You get your theme installed along with a few plugins.

Everything is ready to go and then you start creating content.

Then you look at your page and all the styling and there are paragraph tags everywhere. You might be thinking What did I do?

The realty is WordPress drops p tags everywhere. So how do we solve?

Simple we add some simple code to your functions.php. (Found in your themes folder)

remove_filter( 'the_content', 'wpautop' );

remove_filter( 'the_excerpt', 'wpautop' );

wpautop will take the pee out of your WordPress. The_content is your page and the_excerpt is your post.

Now get coding!



1 2 3 next ... end

Tags