php documents please, Exams of Computer science

the notes of php from n phsakgfdsdhfdhfhdhguifdhidhvds

Typology: Exams

2016/2017

Uploaded on 04/10/2017

surabhi-purwar
surabhi-purwar 🇬🇧

1 document

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Download the code used in the lesson.
PHP_introduction.zip
Instructions to open zip file. The instructions file is present in every zip folder also.
Extract the files.
Open netbeans.
Click on file in the top menu.
Choose New Project option.
Choose PHP category and in Projects section, choose PHP Application with Existing
Source.
For Site Root, browse to the folder where you have extracted these files.
Click on finish.
The folder will open in netbeans.
Note: echo statement is not case sensitive. ECHO/echo/Echo means the same in PHP.
Boolean variables
Boolean variables contain only TRUE or FALSE as values.
$var = TRUE;
This is boolean variable. TRUE or FALSE should not be in quotes. Then it will be considered as
strings.
Arithmetic Operations
It usually includes addition, subtraction,multiplication, division, modulus which gives the
remainder of the division and exponentiation.
Example Name Result
$a + $b Addition Sum of $a and $b
$a - $b Subtraction Difference of $a and $b
$a * $b Multiplication Product of $a and $b
$a / $b Division Quotient of $a and $b
$a % $b Modulo Remainder of $a divided by $b
$a ** $b Exponentiatio
n
Result of raising $a to the $b th power. Introduced in PHP 5.6
Logical Operations
Logical operators, Relational operators are used for comparisons. These are the valid operators
for PHP. These operations either evaluate to TRUE or a FALSE state.
Example Name Result
$a and
$b
And TRUE if both $a and $b are TRUE, FALSE otherwise
$a || $b Or TRUE if either $a or $b is TRUE. If $a is TRUE and $b is false then also this
condition will output true as one of the two options or both the option should
be true in OR case
$a xor
$b
Xor TRUE if either $a or $b is TRUE, but not both; otherwise FALSE
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download php documents please and more Exams Computer science in PDF only on Docsity!

Download the code used in the lesson. PHP_introduction.zip

Instructions to open zip file. The instructions file is present in every zip folder also.

  • Extract the files.
  • (^) Open netbeans.
  • Click on file in the top menu.
  • Choose New Project option.
  • Choose PHP category and in Projects section, choose PHP Application with Existing Source.
  • For Site Root, browse to the folder where you have extracted these files.
  • Click on finish.
  • The folder will open in netbeans.

Note : echo statement is not case sensitive. ECHO/echo/Echo means the same in PHP.

Boolean variables

Boolean variables contain only TRUE or FALSE as values. $var = TRUE; This is boolean variable. TRUE or FALSE should not be in quotes. Then it will be considered as strings.

Arithmetic Operations It usually includes addition, subtraction,multiplication, division, modulus which gives the remainder of the division and exponentiation.

Example Name Result $a + $b Addition Sum of $a and $b $a - $b Subtraction Difference of $a and $b $a * $b Multiplication Product of $a and $b $a / $b Division Quotient of $a and $b $a % $b Modulo Remainder of $a divided by $b $a ** $b Exponentiatio n

Result of raising $a to the $b th power. Introduced in PHP 5.

Logical Operations Logical operators, Relational operators are used for comparisons. These are the valid operators for PHP. These operations either evaluate to TRUE or a FALSE state.

Example Name Result $a and $b

And TRUE if both $a and $b are TRUE, FALSE otherwise

$a || $b Or TRUE if either $a or $b is TRUE. If $a is TRUE and $b is false then also this condition will output true as one of the two options or both the option should be true in OR case $a xor $b

Xor TRUE if either $a or $b is TRUE, but not both; otherwise FALSE

! $a Not TRUE if $a is not TRUE, otherwise FALSE $a % $b Modul o

Remainder of $a divided by $b

http://www.mytoptutorials.com/php/tag/php-multidimensional-arrays/

Difference between single quotes and double quotes:

Single Quotes Double Quotes Double quotes inside single quotes will be considered as strings.

Single quotes inside double quotes will be considered as strings. Variables inside single quotes will be considered as strings and will display the variable name only.

Variables inside double quotes will be considered as variables only and will display the value of the variable instead of name. Input: $variable = 5; echo 'The output is $variable.';

Output: The output is $variable.

Input: $variable = 5; echo "The output is $variable.";

Output: The output is 5.

Commonly used string functions are:

  • (^) strrev($string): Returns the reversed string.
  • strtolower($string): Returns the string with all the characters in lowercase only.
  • strtoupper($string): Returns the string with all the characters in uppercase only.
  • strpos($string,$find_substring,start): This fucntion finds the position of the first occurrence of a substring in a string. e.g. Input: echo strpos("I am a web developer", "web", 1); Output: 7.

Consider the following link of official bootstrap manual for all possible string functions in PHP. Sting Functions

If condition could be used without else part. if($marks == 40){ echo "Just passed"; }

If-else statement can be used inside another if else statement.

  1. if($marks >= 40){
  2. if($marks == 40){
  3. echo "Just passed";
  4. }else{
  5. echo "passed";
  6. }

Note : Please find the solution of the code challenge in the helper text of previous two lessons of

this topic

uidelines : Form tag contains two attributes: action and method.

  1. Action attribute consists the name of the file to which the form data will be sent. This attribute may or maynot contain any value. If no value is mentioned or this attribute is not mentioned in the form tag, the data will be sent to this page only.
  2. Method attribute can have two values: GET or POST.

Difference between GET and POST:

GET POST Values appear in the URL Values do not appear in the URL Values can be bookmarked Values can not be bookmarked There is a limit on characters which can be sent through URL.

There is no limit on the characters send through POST request. Less secure More secure

Code and explanation of user_registration.php

Instructions:

  • Create index.php file.
  • Make a form with action attribute user_registration_script.php and method POST.
  • The form would have email, first name, last name and phone fields.
  • Create user_registration_script.php file.
  • Establish the connection to the database.
  • Store the users data in the variables by using $_POST variable.
  • (^) Store insert query in a variable.
  • Use mysqli_query function to run the insert query.

Note : Please find the solution of the code challenge in the helper text of previous two lessons of this topic.

Header function

The header() function sends a raw HTTP header to a client. In simple words, header redirects the user to another page. Suppose we want logged out user to open only index.php page. If logged out user tries to open any other page, it should be redirected to index.php page.

Code:

Header function takes a string parameter (location: name_of_the_web_page.php).

Header function should be used before we echo or display any HTML element because user will be redirected as header fucntion is encountered in the code.

Code Challenge:

Store page views in the session variable. Instructions:

  • Create index.php file.
  • (^) Start session using session_start().
  • Check whether $_SESSION['views'] is set. If yes, increment the value stored in the session by 1. Else, initialize views session and store value 1 in it.
  • Echo the value of the session.

Code Challenge:

Store page views in the session variable. Instructions:

  • Create index.php file.
  • Start session using session_start().
  • Check whether $_SESSION['views'] is set. If yes, increment the value stored in the session by 1. Else, initialize views session and store value 1 in it.
  • Echo the value of the session.
  • Your Code
  • Solution

1

2

3

4

HTML5 validations 5

6

7

8

9

10

11

12

13

14

15

16

Signup Form 17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

Submit 33

34

Q. Can you explain me in simple terms the use of the isset() function?

isset() function is used to check if the variable is set with some value. For example:

if(isset(($_POST['form-username'])) { //run the script in brackets }

  • The example here means if the field "username" in the form is set to some value then only run the script in brackets.
  • If the field is not set with some value and is empty then skip running the script.
  • Isset is usually used to check if user has entered some value to the variable/field in the form. see more

Q. Can isset() function be used to redirect php page to another php page?

The isset() function is used when there are functions to be performed if any input type is set. We always use isset with loops. E.g.

if(isset($_POST['submit']){ -- code --}.

It cannot redirect php pages to another but can be used as a condition to redirect. E.g.

if(isset($_POST['submit']){ header('location:home.php'); exit; }

This code will redirect the web page if the submit button is set to home.php page.

see more

Q. In HTML forms, what's the difference between using the GET method versus POST? Practically when should we use POST and GET?

  • The basic difference in using GET and POST method is the security.
  • GET method will send the variables through a URL which is visible to users and can be manipulated. POST method will send the values from one page to another in a hidden way and this is not visible to users.
  • You should use POST when you are using forms in your web page, i.e inserting the values in the database. POST method is the best method in that case.
  • GET should be used for searching purposes. E.g. whenever you search something on Google, the values you have entered in the search box is visible in the URL of the web page. i.e these queries don't change any values in the database, they just fetch the values. So GET method should be used.
  • Moreover you cannot bookmark the URL if you are using POST request but it is possible using GET method. Suppose you find awesome search result at Google for some word and want to save that link. You will only be able to save if they are using GET method otherwise you have to type the word again and then get search result, you cannot directly jump to the link if it is not passing value as GET method. see more View more

Ask your doubts to the instructor personally if they are not already answered on forum.

Next live chat: Today (7:00 - 8:00 PM)

For any queries, please write to us at [email protected] © Copyright 2016 Internshala