Php Simple Interview Questions

3
What is PHP? PHP: Hypertext Preprocessor is open source server-side scripting language that is widely used for web development. PHP scripts are executed on the server. PHP allows writing dynamically generated web pages efficiently and quickly. The syntax is mostly borrowed from C, Java and perl. PHP is free to download and use. What does a special set of tags <?= and ?> do in PHP? - The output is displayed directly to the browser. What’s the difference between include and require? - It’s how they handle failures. If the file is not found by require(), it will cause a fatal error and halt the execution of the script. If the file is not found by include(), a warning will be issued, but execution will continue. Explain how to submit form without a submit button.? We can achieve the above task by using JavaScript code linked to an event trigger of any form field and call the document.form.submit() function in JavaScript code. Echo vs. print statement.? echo() and print() are language constructs in PHP, both are used to output strings. The speed of both statements is almost the same. echo() can take multiple expressions whereas print cannot take multiple expressions. Print return true or false based on success or failure whereas echo doesn't return true or false. $message vs. $$message in PHP ? $message is a variable with a fixed name. $$message is a variable whose name is stored in $message. If $message contains "var", $$message is the same as $var. Explain the different types of errors in PHP? Notices, Warnings and Fatal errors are the types of errors in PHP Notices: Notices represents non-critical errors, i.e. accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all but whenever required, you can change this default behavior. Warnings: Warnings are more serious errors but they do not result in script termination. i.e calling include() a file which does not exist. By default, these errors are displayed to the user. Fatal errors: Fatal errors are critical errors i.e. calling a non-existent function or class. These errors cause the immediate termination of the script. Explain the importance of the function htmlentities.? The htmlentities() function converts characters to HTML entities. What is the difference between PHP and JavaScript? The difference lies with the execution of the languages. PHP is server side scripting language, which means that it can’t interact directly with the user. Whereas, JavaScript is client side scripting language, that is used to interact directly with the user.. What is the difference between echo, print and printf()?

description

php most common interview related issues

Transcript of Php Simple Interview Questions

What is PHP?PHP: Hypertext Preprocessor is open source server-side scripting language that is widely used for web development. PHP scripts are executed on the server. PHP allows writing dynamically generated web pages efficiently and quickly. The syntax is mostly borrowed from C, Java and perl. PHP is free to download and use.What does a special set of tags <?= and ?> do in PHP? - The output is displayed directly to the browser. What’s the difference between include and require? - It’s how they handle failures. If the file is not found by require(), it will cause a fatal error and halt the execution of the script. If the file is not found by include(), a warning will be issued, but execution will continue.Explain how to submit form without a submit button.?We can achieve the above task by using JavaScript code linked to an event trigger of any form field and call the document.form.submit() function in JavaScript code. Echo vs. print statement.?echo() and print() are language constructs in PHP, both are used to output strings. The speed of both statements is almost the same.echo() can take multiple expressions whereas print cannot take multiple expressions.Print return true or false based on success or failure whereas echo doesn't return true or false.$message vs. $$message in PHP ?$message is a variable with a fixed name. $$message is a variable whose name is stored in $message. If $message contains "var", $$message is the same as $var.Explain the different types of errors in PHP?Notices, Warnings and Fatal errors are the types of errors in PHPNotices: Notices represents non-critical errors, i.e. accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all but whenever required, you can change this default behavior.Warnings: Warnings are more serious errors but they do not result in script termination. i.e calling include() a file which does not exist. By default, these errors are displayed to the user.Fatal errors: Fatal errors are critical errors i.e. calling a non-existent function or class. These errors cause the immediate termination of the script.Explain the importance of the function htmlentities.?The htmlentities() function converts characters to HTML entities.What is the difference between PHP and JavaScript?The difference lies with the execution of the languages. PHP is server side scripting language, which means that it can’t interact directly with the user. Whereas, JavaScript is client side scripting language, that is used to interact directly with the user..What is the difference between echo, print and printf()? Echo is the basic type used to print out a string. It just shows the content of the message written using it. It can have multiple parameters as well. print is a construct, it returns TRUE on successful output and FALSE there is no output. It can’t have multiple parameters. Printf() is a function, and not be used as a construct. It allows the string output to be formatted. It is the slowest medium to print the data out. What is Validation?The term “validation” means to check input submitted by the user. The form is the input text boxes, radio boxes, check boxes and other web controls used to collect data. Validation is performed after the client enters all the required data and presses the “Submit” button. If any of the data entered does not match a required pattern, it’s missing or it’s wrongly entered, the information is not stored and returned back with an error message. The user will then have to edit the input before re-submission.Why is Validation Required?Validation has become an important part of web development process. It is applied in almost every website which involves user interaction. The reason behind this is to save the workload on the server by preventing unwanted user requests to access information. It checks the user’s authentication and helps to keep the web secure.There are 2 types of Validation:

Client-Side Validation: In client-side validation, validation is performed on the web browser on the client machine. This is possible by using JavaScript.Server Side Validation: In server-side validation, the data is sent to the server when the submit button is pressed. Server-side validation is much more reliable and secure compared to client side validation. All server side scripting languages such as PHP perform this type of validation.Validation performs two functions:Basic Validation: The form is checked to make sure that all data is entered in each form field. It goes through every form field one-by-one and checks the entry. For instance, the Facebook signup form uses basic server-side validation.Data Format Validation: The data entered is checked for format such as an email address, which should contain an “@” symbol and domain. This requires a lot more logic and coding. Most developers use regular expressions to check for format validation.For Example: if the form field accepts integer values, then all the values other than an integer is rejected with an error message. The most widely format validation is used against the textbox field, which requires all types of validation. The reason behind this is that it can store a huge range of alphanumeric values such as phone numbers, email id, and username.Use of Validation:It prevents a user’s unauthorized access.It helps provide genuine data.Client side validation saves servers from unwanted and unnecessary load which may otherwise lead to its crash.Validation prevents the database from being overloaded due to anonymous storage of information.Security can be implemented easily with validation.Validation done using PHP cannot be breached by the user since it is done with the server-side validation.Example of PHP Form Validation<form action="validate.php" method="POST">Name:<input type="text" name="Name" />Email:<input type="text" name="Email" />Number:<input type="text" name="number" /><input type="submit" name="submit” /></form>

PHP Script to Validate: validate.phpif ($_SERVER["REQUEST_METHOD"] == "POST") {$name = $_POST["name"]$email = $_POST["email"];$number= $_POST["website"];}if (empty($name)){echo "<p class=\"error\">Your last name cannot be blank</p>";}

if(empty($email)) )){echo "<p >Your email id cannot be blank</p>";}if(empty($number) || $number <1000000000&& $number>=1000000000){echo “<p>Number should be of 10 digit</p>”}

The above example explains a very basic implementation of validation. There are three entries for which the input is taken from the user. This input is then sent to validate.php file for the validation process. The entered name is checked and if it is empty, the error is returned. Similarly, the remaining two form fields are validated for the same type of content. The error message is displayed on the screen so that the user can fill the entries as per the requirements of the form. This is text field validation.Validation can also be applied to all the other form fields, which are present in HTML. Different form fields require different types of validation. For instance, the RadioButton control is validated to check if it is selected or not.