PHP Basics

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

What is the purpose of the if...else statement?

  • To execute a block of code repeatedly
  • To select one of several blocks of code to be executed
  • To execute some code only if a specified condition is true
  • To execute different blocks of code based on multiple conditions (correct)

What is the syntax for the if statement?

  • if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; }
  • if condition then { code to be executed if condition is true; }
  • if (condition) { code to be executed if condition is true; } (correct)
  • if (condition) then { code to be executed if condition is true; }

What is the purpose of the switch statement?

  • To execute some code only if a specified condition is true
  • To select one of many blocks of code to be executed (correct)
  • To execute different blocks of code based on multiple conditions
  • To execute a block of code repeatedly

What is the effect of the expression $a += 5?

<p>It increments the value of $a by 5 (D)</p> Signup and view all the answers

What is the purpose of the if...else if....else statement?

<p>To execute different blocks of code based on multiple conditions (D)</p> Signup and view all the answers

In PHP, what is the purpose of the $ symbol in a variable name?

<p>To denote a variable (D)</p> Signup and view all the answers

What is the output of the following code: <?php $t=date("H"); if ($t < 20) { echo "Have a good day!"; } else { echo "Have a good night!"; } ?>

<p>Have a good day! (C)</p> Signup and view all the answers

What happens when you add a double to an integer in PHP?

<p>The result will always be a double (C)</p> Signup and view all the answers

What is the purpose of arithmetic operations in PHP?

<p>To perform mathematical operations on variables (D)</p> Signup and view all the answers

What is the purpose of the is_double() function in PHP?

<p>To check if a variable is a double (B)</p> Signup and view all the answers

What is the purpose of concatenation in PHP?

<p>To join strings into one (B)</p> Signup and view all the answers

What is the use of the \n escape sequence in PHP?

<p>To insert a new line character (B)</p> Signup and view all the answers

In PHP, what happens when you try to use a string in an arithmetic expression?

<p>The string is converted to a number up to the first non-numeric character (C)</p> Signup and view all the answers

What is the purpose of type casting in PHP?

<p>To temporarily change the data type of a variable (A)</p> Signup and view all the answers

What is the use of the is_array() function in PHP?

<p>To check if a variable is an array (B)</p> Signup and view all the answers

What is the purpose of the \t escape sequence in PHP?

<p>To insert a horizontal tab (A)</p> Signup and view all the answers

What is the primary use of PHP in web development?

<p>To generate dynamic web pages (C)</p> Signup and view all the answers

How are PHP scripts executed?

<p>On the server-side by the web server (C)</p> Signup and view all the answers

What is the default file extension for PHP files?

<p>.php (A)</p> Signup and view all the answers

What happens when a PHP script is accessed by a client's web browser?

<p>The PHP code is executed on the server-side and the output is sent to the browser (C)</p> Signup and view all the answers

How are string literals defined in PHP?

<p>Using single quotes and double quotes (B)</p> Signup and view all the answers

What is the purpose of variable interpolation in PHP?

<p>To replace variables with their values (A)</p> Signup and view all the answers

What is the similarity between PHP and C/C++?

<p>Syntax and paradigm (C)</p> Signup and view all the answers

What is the purpose of the reserved PHP tag?

<p>To start a PHP script (A)</p> Signup and view all the answers

Flashcards are hidden until you start studying

Study Notes

Arithmetic Operations

  • Subtraction: $a - $b
  • Multiplication: $a * $b
  • Division: $a / $b
  • Assignment operators: $a += 5 (also works for *= and /=)

Concatenation

  • Use a period to join strings into one

PHP Control Structures

  • Control structures: allow us to control the flow of execution through a program or script
  • Grouped into conditional (branching) structures (e.g. if/else) and repetition structures (e.g. while loops)

PHP Conditional Statements

  • if statement: executes some code only if a specified condition is true
  • if...else statement: executes some code if a condition is true and another code if the condition is false
  • if...else if....else statement: selects one of several blocks of code to be executed
  • switch statement: selects one of many blocks of code to be executed

The if Statement

  • Syntax: if (condition) { code to be executed if condition is true; }
  • Example: output "Have a good day!" if the current time is less than 20

The if...else Statement

  • Syntax: if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; }
  • Example: output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise

PHP Details

  • Procedural language
  • C-like syntax: { } ;
  • Extensive Function Library
  • Good Web-server integration
  • Script embedded in HTML
  • Easy access to form data and output of HTML pages
  • Not fully object-oriented

PHP and HTML

  • HTML-embedded
  • PHP scripts are essentially HTML pages with the occasional section of PHP script
  • PHP script is enclosed in the tag pair: &lt;?php ?&gt;

Variables

  • Creation of variables: variable names always start with $ sign
  • Rest of the variable name can contain alphanumeric, underscore, and hyphen
  • Example: $name = “abc”;, $age = 36;
  • PHP is case sensitive

Escape Sequences in PHP

  • \n - Insert a new line character
  • \r - Carriage return
  • \t - Horizontal tab
  • \\ - Backslash
  • \$ - Dollar
  • \" - Double quote

Changing Data Type

  • Type casting process involves dynamically changing the type of data item
  • If we add a double to an integer, the result will be a double even if we are storing it in the original integer variable

Type Checking

  • is_array(var) - returns TRUE if the variable is an array
  • is_double(var) - returns TRUE if the variable is a double
  • is_float(var) - returns TRUE if the variable is a floating point number
  • is_int(var) - returns TRUE if the variable is an integer
  • is_string(var) - returns TRUE if the variable is a string
  • is_object(var) - returns TRUE if the variable is an object

What is PHP?

  • "PHP: Hypertext Preprocessor"
  • Open-source, server-side scripting language
  • Used to generate dynamic web-pages
  • PHP scripts reside between reserved PHP tags
  • Interpreted language, scripts are parsed at run-time rather than compiled beforehand
  • Executed on the server-side
  • Source-code not visible by client

What Can PHP Do?

  • Generate dynamic page content
  • Create, open, read, write, delete, and close files on the server
  • Collect form data
  • Send and receive cookies
  • Add, delete, modify data in your database
  • Control user-access
  • Encrypt data

What does PHP code look like?

  • Structurally similar to C/C++
  • Supports procedural and object-oriented paradigm (to some degree)
  • All PHP statements end with a semi-colon
  • Each PHP script must be enclosed in the reserved PHP tag

Comments in PHP

  • Standard C, C++, and shell comment symbols
  • // C++ and Java-style comment
  • # Shell-style comments

String Handling

  • String literals (constants) enclosed in double quotes “ ” or single quotes ‘ ’
  • Within “”, variables are replaced by their value: – called variable interpolation

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team
Use Quizgecko on...
Browser
Browser