PHP Error Control Operator In Hindi | Error Control In PHP

Blogs ❯❯ PHP

Image could not load

Error Control In PHP

PHP Error Control

PHP एक error control operator - at sign (@) को भी support करती है , इसे sign को किसी expression से पहले prepend किया जाता है, और अगर उस expression में कोई diagnostic error generate हुई तो suppress / ignore हो जाएगी।

For Example

<?php $x = $arr[0]; // Error : PHP Warning: Trying to access array offset on value of type null. ?>

ऊपर दिए गए example में अगर , कोई Array variable define नहीं है still उसे access करने का try किया तो कुछ इस तरह से error आयी। इससे बचने के लिए आप simply @ prepend कर सकते हैं।

जैसे

<?php $x = @$arr[0]; ?>

Get Error Message

अब error तो control हो गयी , लेकिन अब अगर आपको error message पता करना है तो आप error_get_last() function को call कर सकते हैं जो last error के बारे men एक Associative Array return करता है।

<?php $x = @$arr[0]; print_r(error_get_last()); ?>

Output

Array
(
    [type] => 2
    [message] => Trying to access array offset on value of type null
    [file] => C:\Users\verma\Desktop\test.php
    [line] => 2
)

अब इस array से message key को आप easily access करके error message print करा सकते हैं।

Important

Internally @ operator , उस particular line के लिए error reporting level 0 set करता है , जिससे सिर्फ warning और parse error ही ignore होती हैं। हालाँकि Fatal Error को इससे नहीं रोका जा सकता है।

जैसे कि नीचे दिए गए example में undefined function test() को call किया गया।

<?php $x = @test(); // Fatal error: Uncaught Error: Call to undefined function test() ?>

जहाँ तक हो सके Error Control Operator @ का use कम ही करें , इससे execution और performance slow होता है , हालाँकि आप need के according variables को एक default value set कर सकते हैं।

Recent Blogs

Loading ...

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook