PHP Basics Flashcards
What is PHP?
HyPertext Preprocessor
A server side scripting language designed to interact with html
Interpreted by a scripting engine
Cross-platform, free and open source
Is PHP a strongly typed language or a loosely typed language?
Loosely typed - you don’t need to specify whether a variable is an int, String etc. PHP figures it out from the value you assign it
What are the rules for Variable names?
Always begin with $
Case Sensitive
Can include:
- letters
- numbers
- underscores
Can’t include
- spaces
Which one can take multiple parameters - print or echo?
Echo
What is a Constant?
Something that is set once and never changed
What are the rules for defining Constant names
Don’t need to start with $
Convention is to use capital letters for variable
Set using define
- define(“CONSTANT_NAME”, value);
What is the difference between Single quotation marks and Double quotation marks
Single:
- values will be treated LITERALLY
- escaped characters will be displayed exactly as typed
Double:
- values will be INTERPRETED
- escaped characters will have their represented values printed
What does it mean to escape a character, and how do you do it?
It takes away any special meaning of the following character
Use \
What does Type Casting mean, and how is it done in PHP?
Casting variables from one type to another
Normally done automatically in PHP:
$age_string = “18”;
$new_age = $age_string +1;
It knows that $new_age is an int not a string
Can be done manually:
$age_string = “21”;
$age_int = (int)$age_string;
What is the difference between == and ===?
== means equal to (check if the values of the operands are equal or not)
=== means identical to (check if the values AND the types of the operands are the same)
Are Arrays statically defined? Why/why not?
No
- You don’t need to know size in advance
- Can store any type of object in one array (eg can contain both int and String)
- Elements can be added or deleted at any time
Which of the below is the correct way to define an array?
- $students = array();
- $students = array(“Ali”, “Mike”, “Lucy”, “Peter”);
- $students[] = “Faye”;
All of them!
What are the three types of Arrays?
Enumerated
Associative
Multidimensional
What is an enumerated array?
An array that is numerically indexed
If not specified, the first element is at index 0
Can set which position each element should occupy, e.g:
$students= array(1=>“ali”, 2=>“Mike”, 4=>“lucy”, 9=>“Peter”);
What is an associative array?
An array that is indexed by strings
Specify a string instead of a number index within the square brackets
Each element can be referenced using the string index associated with the element
String index must be quoted
The string index is called the key
Creates a key/value pair
What is a multidimensional array?
An array consisting of other arrays
What are the rules for defining functions?
Must be within the php tags
Must begin with a letter or an underscore
Name must be unique
Can contain letters, numbers and underscores
Case-sensitive
Where should you place function definitions?
At the top of a script or in separate files
When should you use a function?
When you have code that might be executed in multiple places in the script
What is a return statement?
They return a value to the statement that called the function
A function with no return statement implicitly returns NULL
How do you call a function?
Writing the function’s name and parameter values in parentheses separated by commas
What is auto-loading and why is it useful?
Allows you to keep scripts in separate files and then include them in the main PHP script
Entire contents of the included file is inserted in the calling script
Useful for defining functions needed by multiple pages
What are the calling functions for including files?
include(“filename”) - if the file is not found, it produces a warning (E_WARNING) and the script will continue
require(“filename”) - will produce a fatal error (E_COMPILE_ERROR) and stop the script if the file is not found
Should you include things that could be done in html within the php brackets?
No
Use PHP as little as possible
HTML takes less time to process than PHP