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