PHP Basics Flashcards

1
Q

What is PHP?

A

HyPertext Preprocessor

A server side scripting language designed to interact with html

Interpreted by a scripting engine

Cross-platform, free and open source

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Is PHP a strongly typed language or a loosely typed language?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the rules for Variable names?

A

Always begin with $

Case Sensitive

Can include:

  • letters
  • numbers
  • underscores

Can’t include
- spaces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which one can take multiple parameters - print or echo?

A

Echo

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a Constant?

A

Something that is set once and never changed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the rules for defining Constant names

A

Don’t need to start with $

Convention is to use capital letters for variable

Set using define
- define(“CONSTANT_NAME”, value);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between Single quotation marks and Double quotation marks

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does it mean to escape a character, and how do you do it?

A

It takes away any special meaning of the following character

Use \

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does Type Casting mean, and how is it done in PHP?

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference between == and ===?

A

== 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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Are Arrays statically defined? Why/why not?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which of the below is the correct way to define an array?

  • $students = array();
  • $students = array(“Ali”, “Mike”, “Lucy”, “Peter”);
  • $students[] = “Faye”;
A

All of them!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the three types of Arrays?

A

Enumerated
Associative
Multidimensional

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is an enumerated array?

A

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”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is an associative array?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a multidimensional array?

A

An array consisting of other arrays

17
Q

What are the rules for defining functions?

A

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

18
Q

Where should you place function definitions?

A

At the top of a script or in separate files

19
Q

When should you use a function?

A

When you have code that might be executed in multiple places in the script

20
Q

What is a return statement?

A

They return a value to the statement that called the function

A function with no return statement implicitly returns NULL

21
Q

How do you call a function?

A

Writing the function’s name and parameter values in parentheses separated by commas

22
Q

What is auto-loading and why is it useful?

A

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

23
Q

What are the calling functions for including files?

A

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

24
Q

Should you include things that could be done in html within the php brackets?

A

No

Use PHP as little as possible

HTML takes less time to process than PHP