Docsity

PHP Strings and Error Handling: A Comprehensive Guide, Slides of Web Design and Development

Partial preview of the text

Download PHP Strings and Error Handling: A Comprehensive Guide and more Slides Web Design and Development in PDF only on Docsity!

PHP Strings

 They are sequences of characters, like "PHP supports string operations".  Following are valid examples of string $string_1 = "This is a string in double quotes"; $string_2 = "This is a somewhat longer, singly quoted string"; $string_39 = "This string has thirty-nine characters"; $string_0 = ""; // a string with zero characters

String cont..

Singly quoted strings are treated almost literally, whereas doubly quoted strings replace variables with their values as well as specially interpreting certain character sequences.

String Concatenation Operator

 To concatenate two string variables together, use the dot (.) operator: 

Types of Errors in PHP

An error is a type of mistake. We can say an error is a condition of having incorrect or false knowledge or an error is defined as an unexpected, invalid program state from which it is impossible to recover. Error can also be defined as "a deviation from accuracy or correctness". A "mistake" is an error caused by a fault: the fault being misjudgment, carelessness, or forgetfulness. An error message with filename, line number and a message describing the error is sent to the browser.

1.Parse Errors (syntax errors) The parse error occurs if there is a syntax mistake in the script; the output is Parse errors. A parse error stops the execution of the script. There are many reasons for the occurrence of parse errors in PHP. The common reasons for parse errors are as follows: Common reason of syntax errors are: Unclosed quotes Missing or Extra parentheses Unclosed braces Missing semicolon

 Example

 Output In the above code we missed the semicolon in the second line. When that happens there will be a parse or syntax error which stops execution of the script, as in the above image:

Example

Output In the above code we defined a function fun1 but we call another function fun2 i.e. func2 is not defined. So a fatal error will be produced that stops the execution of the script. Like as in the above image.

  1. Warning Errors Warning errors will not stop execution of the script. The main reason for warning errors are to include a missing file or using the incorrect number of parameters in a function. Example

Output In the above code we include a welcome.php file, however the welcome.php file does not exist in the directory. So there will be a warning error produced but that does not stop the execution of the script i.e. you will see a message Warning Error !!. Like in the above image: