PHP is a widely used open source server-side programming language which runs on various platforms. This post contains notes on the chapter in a question-answer format for easier comprehension.
- Name a few programming languages for server-side programming.
- Java and JSP
- What are the features of PHP?
- Simple: It is very simple and easy to use, as compared to other scripting languages.
- Interpreted: It is an interpreted language, i.e. there is no need for compilation.
- Faster: It is faster than other scripting language e.g. JSP and ASP.
- Open source: User do not need pay for use of PHP. You can freely download and use.
- Platform independent: PHP code can be run on any platform like Linux, Unix, MAC OS X, Windows.
- Case sensitive: PHP is case sensitive scripting language while declaration of variables and its use in the program. In PHP, all keywords Classes, Functions and User-defined functions are not case-sensitive.
- Error reporting: PHP have some predefined error reporting constants to generate a warning or error notice.
- Real-time access monitoring: PHP provides access logging by creating the summary of recent accesses for the user.
- Loosely typed language: PHP supports variable usage without declaring its data type. It will be taken at the time of execution based on the type, data has its value.
- Write the steps to execute a PHP Program.
- Type the program and save it as “.php”extension using any text editor (for e.g. “HelloWorld.php”).
- Create folder servers root directory. Like PHP_ folder in /var/ www/html.
- Save the ‘.php’ file in root directory folder. E.g ‘HelloWorld ‘ file in “PHP_ folder” folder.”
- Go to browser and type “http://localhost/folder_name/”inurl bar.
- Click on ‘.php’ file.
Eg. “http://localhost/folder_name/” and click on “HelloWorld.php” file.
- What are PHP Variables?
- Variable is a symbol or name that stands for a value.
- Variables are used for storing values such as numeric values, characters, character strings, or memory addresses, so that user can be used in any part of the program.
- Write a note on the Rules for declaring PHP Variable.
- A Variable starts with the $ sign, followed by the name of the variable.
- A Variable name must start with a letter or the underscore character.
- A Variable name cannot start with a number.
- A Variable name can only contain Alpha-Numeric characters and underscores (a-z, 0-9, and _ ).
- Variable names are case-sensitive.
- List the all data types that PHP language can support.
Data Types supported by PHP:
- What are comments in PHP?
- Comments are the statements in PHP code which are not visible in the output of the program.
- It is ignored during execution
- How do we stop deleting local variable after function execution?
- When a function is executed in PHP, then all of its variables are deleted.
- In some cases, if user a wants a local variable not to be deleted then the use of “STATIC” keyword is must.
- When do we use if-else statements?
- The if-else statement is used when the programmer has to make a decision based on either this or that conditions.
- What is the use of the If statement in PHP?
- The ‘If statement’ allows programmer to make decision, based on one or more conditions; and execute a piece of code conditionally.
- SYNTAX :
block of statement;
- What is used of the PHP function strpos()?
Ans. PHP function strpos() searches for a specific text within a string and returns the character position of the first match. If no match is found, then it will return false.
- Why do we use trim() function in PHP?
Ans. PHP function trim() is used to remove whitespace and other predefined characters from both sides of a string.
- What is an array and how we can create it in PHP?
- An array is a special variable, which can hold more than one value at a time.
- It can store multiple values in one single variable.
- Syntax :
$a = array( values )
- What are different types of arrays?
Three types of arrays in PHP are:
- Indexed Arrays –arrays with a numeric index
- Associative Arrays –arrays with named keys
- Multi-Dimensional Arrays –arrays containing one or more arrays
- What is an index array?
Ans. The index can be assigned automatically (index always starts at 0).
$a = array( value1, value2, …, value n)
- What is an associative array?
Ans. Associative arrays are arrays that use named keys instead of index to identify record/value.
$a = array( key1 => value1, key2=>value2, …,key n => value n)
- What are PHP User Defined Functions?
Ans. A user-defined function declaration starts with the word function.
code to be executed;
- Write key the difference between GET and POST Method.
$_GET is an array of variables passed via the URL parameters.
$_POST is an array of variables passed via the HTTP post method.
- Write difference between GET and POST Method in PHP.
i. Information sent from a form with the GET method is visible to everyone
i. Information sent from a form with the POST method is invisible to everyone
ii. It has limits on the amount of information to send.
ii. POST has no limits on the amount of information to send
iii. GET may be used for sending non- sensitive data
iii. POST may be used for sending sensitive data.
- Write a short note on POST.
- Information sent from a form with the POST method is invisible to everyone.
- POST Method has no limits on the amount of information to send.
- POST method supports advanced functionality such as support for multi-part binary input while uploading files to server.
- The variables are not displayed in the URL so it is not possible to bookmark the page.
- How do we create Database Connection object in PHP?
- Example: $conn = new PDO($servername, $username, $password);
- First, we connect PHP with PostgreSQL server.
- The above statement creates connectivity object ‘$conn’ with three parameters as shown below:
- Server host name or ip address and database name
- Database username
- Database password
- What is a session in PHP?
- PHP session is used to store user information on server to track user activities.
- It helps web applications to maintain user information on all the pages.
- If user logs in to their Gmail account, the session helps us to access the Youtube account also.
- When do we use loop structure in PHP?
- Loops are used to execute the same block of code repeatedly as long as a certain condition is satisfied.
- Syntax :
for(initialisation;condition;incrementation or decrementation)
- What is the use of foreach loop?
- The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.
- Syntax :
foreach ($array as $value)
code to be executed;