Stay updated...

Month: July 2022

Handling All PHP Errors

Sometimes your PHP application might produce many different types of earnings and errors or sometimes you might see a blank page which might not understand if you are getting an error or not.

In this article, we will discuss all PHP errors, warnings and how you can turn on/off these errors. So if you are having problems with your PHP application and need to display all the errors and warnings, you are in the right place, we will also discuss how to event collect them when your site is now online.

Display all PHP errors

ini_set('display_errors', 1);
error_reporting(E_ALL);

Now, let’s explain what these lines mean in plain text

ini_set function

The ini_set function allows you to override the configuration found in your PHP.ini file. The display_errors option will determine if the errors will be displayed or hidden. It’s important that this error mode be turned off during production.

So how can you use this? If you want to display errors you simply pass a value 1 to hide errors and pass a value 0

ini_set('display_errors', 1); //display errors 
ini_set('display_errors', 0); //hide errors

Note: Using this won't be able to display parse errors such as missing semicolons or missing curly braces. In this case, you have modifiy your PHP ini configuration

Setting up your PHP.ini to display errors

If you can’t see errors after using the ini_set to display errors, don’t worry you can still do that by going to your PHP.ini file

How to locate your PHP.ini file

If you are using xampp, you can find your PHP.ini file with these steps.

  • Open the directory where you install your xampp.
  • Look for a folder called PHP
  • In the PHP folder, you can scroll down or use the search option to locate the php.ini file.

Turn on display errors in the PHP.ini file

If you have successfully located the PHP.ini file bravo, all we have to do is to open the file with a text editor and search for display_errors then we change its value to on.

display_errors = on

Note: After we have made a change on the php.ini file and saved the file, we must restart our server.

Display PHP Errors via .htaccess Configuration

You can also enable or disable error mode using the .htaccess file located in the root or public directory of the project

php_flag display_startup_errors on 
php_flag display_errors on

This is the same as you add to the PHP code to show PHP errors. Depending on which files you have access to and how you do deployments and server configurations, you may want to configure display_erros in .htaccess or your PHP.ini file. Many hosting providers will not allow you to modify your PHP.ini file to enable display_errors.

In the .htaccess file, a custom error log can also be enabled as long as the log folder or the log file is writable by the web server. The log file can be a relative path to where the .htaccess is located, or it can be an absolute path such as

/var/www/html/website/public/logs

php_value error_log logs/all_errors.log

PHP Warnings and Notices

Most times, the warnings don’t affect our application but will cause some fatal errors in certain conditions. So these errors must be fixed because this means that the application won’t run normally under certain scenarios. In case these warnings cause a lot of errors, then it would be more practical to hide the errors and just show the warning messages.

error_reporting(E_WARNING);

Hiding and showing a warning is just as simple as adding a single line of code. To show warnings and notices, the parameter for the error reporting function will be E_WARNING | E_NOTICE.

The error_reporting function can also accept E_ERROR and E_PARSE parameters as bitwise operators. To report all errors except for notices, then the parameter is E_ALL & ~E_NOTICE where E_ALL stands for all possible parameters of the error reporting function.

error_reporting(0);

To remove all errors, warnings, and parse message notices, the parameter that should be passed to the error_reporting function is zero (0). It would be not practical to have this line of code in each of the PHP files. it would be better to turn off report messages in the PHP ini file or in the .htaccess.

error_reporting(E_NOTICE);

PHP allows variables to be used even when not declared. This is not standard practice because undeclared variables will cause issues for the application once it is used in loops and conditions.

Sometimes this also happens because the declared variable has a different spelling than the variable being used for conditions or loops. When E_NOTICE is passed in the error_reporting function, then these undeclared variables will be displayed in the web application.

error_reporting(E_ALL & ~E_NOTICE);

the error reporting function allows you to filter which errors can be shown. The “~” character means “note” so the parameter ~E_NOTICE means not to show notices. Notice the “&” and “|” characters in between the possible parameters. The “&” character is for “true for all”, while the “|” character represents either one as long as it is true. These two characters have the same meaning in PHP conditions OR and AND.

Hope this helps you understand how to handle PHP warnings and errors, if you find anything which is not clear you can leave a comment below.

Thanks.

10 awesome extensions VSCode for Laravel/PHP

10 Awesome VS Code Extensions for PHP/Laravel Developers

We all know how text editors are very important to us when we code, for everyone who is getting started with Laravel.

Below, are my list of the Top 10 extensions I recommend for all Laravel/PHP developers, which will help you expand your productivity.

1. Laravel Artisan

We all use the VS Code terminal to run artisan commands and that is great but using the Laravel Artisan Extension you can run the artisan commands from within VS Code directly.

Get a List of Route
Make controller

Some of the main features are:

  • Make files (Controllers, Migrations, Models, etc)
  • Run Custom Commands
  • Manage the database
  • Clear Caches
  • Generate Keys
  • View all routes
  • Start/stop a local PHP server for test purposes

Laravel Artisan on VS Code

2. Laravel Blade Snippets

Laravel Blade Snippets extensions add syntax highlight support

Laravel blade support

Some of the features are

  • Blade syntax highlight
  • Blade snippets
  • Emmet works in blade template
  • Blade formatting

After that is done, there is some other stuff to be done for the extension to work properly. Go to File -> Preferences -> Settings and add this to your settings.json

"emmet.triggerExpansionOnTab": true, // enable tab to expanse emmet tags
"blade.format.enable": true,         // if you would like to enable blade format
"[blade]": {
    "editor.autoClosingBrackets": "always"
},

For more information on how to use the Laravel Blade Snippets make sure to your check here documentation here.

Laravel Blade Snippets on VS Code

3. Laravel Blade Spacer

Laravel blade spacer helps to automatically add spacing to blade templating markers.

Laravel Blade Spacer

Supports the following tags

  • {{ }}
  • {!! !!}
  • {{-- --}}

Laravel Blade Spacer

4. Laravel Extra Intellisense

Laravel Extra Intellisense provides Laravel route, views and autocompletes for VS Code

Route names and route parameters

Some of the autocomplete features are

You can also check the documentation on how to set it up.

Laravel Extra Intellisense

5. Laravel goto view

This is one of my favourite extensions when developing in Laravel, this extension helps you go to a particular view when clicked.

how to use

How to use this extension

You can use Ctrl or Alt + click to jump to the first matched Blade View file

Laravel goto View on VS Code

6. DotENV

DotENV extensions helps a lot in VS Code .env sytnax highlighting

DotENV Sample

You can check DotENV documentation for more information.

DotENV on VS Code

7. Laravel Snippets

Laravel snippets extension for VS Code Support Laravel 5 and above, this snippet provides prefix follows Laravel Facades for example Route:: Request::

Support Snippets Prefix

  • Auth
  • Broadcast
  • Cache
  • Config
  • Console
  • Cookie

Browse the documentation for more about the Laravel Snippets extensions

8. Laravel goto controller

As your application grows, the number of your Controllers grows as well, so at some point, you might end up with hundreds of controllers. Hance finding your way around might get tedious.

This is the exact problem that the Laravel-goto-controller VScode extension solves.

The extension allows you to press Alt + click on the name of the controller in your routes file, and it will navigate you from the route to the respective controller file:

Laravel goto controller

For more information, make sure to check the documentation here:

VSCode extension for Laravel

9. PHP Namespace Resolver

PHP Namespace Resolver can import and expand your class. You can also sort your imported classes by line or in alphabetical order.

PHP namespace resolver

For more details, you can check the online documentation on more configurations

PHP Namespace Resolver on VS Code

10. PHP Formatter

After installing as an extension with Visual Studio Code, this extension automatically formats your PHP code, in accordance with PSR-0, PSR-1, PSR-2 or Symfony style conventions.

PHP formatter

Some of the features are:

  • Format current selection only, or the whole file.
  • Trigger formatting with custom keybindings or actions.
  • Supports formatting on save.
  • Supports adjustable level (i.e. PSR2) and fixers.
  • Can be configured to support other file extensions than PHP as well, i.e. “.inc” files.
  • Supports different PHP-CS-Fixer installation methods, i.e. Composer vs manual installation.

For more on how to set up and use the PHP formatter on VS Code, you can go through the online documentation

PHP Formatter on VS Code

Conclusion

If you like all those extensions, you can take a look at the Laravel Extension Pack on VS Code, where you could get all of the mentioned extensions as 1 bundle!

The only extension not included in the pack is the Laravel Blade Spacer, so make sure to install it separately!

I hope that this helps!

Powered by WordPress & Theme by Anders Norén