Semis Lecture 1 Flashcards
___ is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
PHP
PHP is a ___, and a powerful tool for making dynamic and interactive Web pages.
server scripting language
PHP is a widely-used, free, and efficient alternative to competitors such as ___.
Microsoft’s ASP
sample php code block
<html>
<body>
< ?php
echo "My first PHP
script!"; ? >"
</body>
</html>
PHP is an acronym for…
“PHP: Hypertext Preprocessor”
PHP is a widely-used, …
open source scripting language
___ are executed on the server
PHP scripts
PHP is free to ___
download and use
What is a PHP File? PHP files can contain… (5)
text, HTML, CSS, JavaScript, and PHP code
___ are executed on the server, and the result is returned to the browser
as plain HTML
PHP code
PHP files have extension…
“.php”
What Can PHP Do?
* PHP can generate ___
* PHP can ___ on the server
* PHP can collect ___
* PHP can ___ cookies
* PHP can ___ in your database
* PHP can be used to ___
* PHP can ___
dynamic page content;
create, open, read, write, delete, and close files;
form data;
send and receive;
add, delete, modify data;
control user-access;
encrypt data
With PHP you are not limited to ___. You can output (a), (b), and even (c). You can also output any text, such as (d) and (e).
output HTML; (a) images, (b) PDF files, (c) Flash movies, (d) XHTML, (e) XML
Why PHP?
* PHP runs on ___ (Windows, Linux, Unix, Mac OS X, etc.)
various platforms
Why PHP?
* PHP is compatible with ___ (Apache, IIS, etc.)
almost all servers used today
Why PHP?
* PHP supports a ___
wide range of databases
Why PHP?
* PHP is ___. Download it from the official PHP resource: www.php.net
free
Why PHP?
* PHP is ___ and ___ on the server side
easy to learn; runs efficiently
To start using PHP, you can:
* Find a ___ with PHP and MySQL support
* Install a ___ on your own PC, and then install PHP and MySQL
web host; web server
Use a Web Host with PHP Support
* If your server has activated support for PHP…
* Just create some ___, place them in your web directory, and the server will automatically parse them for you.
* You do not need to compile anything or install any extra tools.
* Because PHP is free, most ___ offer PHP support.
* Set Up PHP on Your Own PC
you do not need to do anything;
.php files;
web hosts
However, if your server does not support PHP, you must:
* install a (a)
* install (b)
* install a (c), such as MySQL
(a) web server;
(b) PHP;
(c) database
Basic PHP Syntax
* A ___ can be placed anywhere in the document.
* A PHP script starts with ___ and ends with ___
PHP script; <?php & ?>
sample of basic php syntax
<?php
// PHP code goes
here ?>
Example
<html>
<body>
<h1>My first PHP page</h1>
</body>
</html>
- PHP statements end with a ___
semicolon (;)
A ___ in PHP code is a line that is not read/executed as part of the program. Its only purpose is to be ___ who is looking at the code.
comment; read by someone
Comments can be used to:
* Let others…
understand what you are doing
Comments can be used to:
*
Most programmers have experienced coming back to their own work a year or two later and having to re-figure out what they did. Comments can remind you of what you were thinking when you wrote the code
Remind yourself of what you did
php code snippet with comments example
Example
<html>
<body>
</body>
</html>
Example
<html>
<body>
</body>
</html>
does all those echos work?
yes
In the example below, only the first statement will display the value of the $color variable (this is because $color, $COLOR, and $coLOR are treated as three different
variables):
Example
<html>
<body>
</body>
</html>
when it comes to variables, php is case-sensitive
Data Types: Variables can store data of different types, and different data types can do different things. PHP supports the following data types: (8)
- String
- Integer
- Float (floating point numbers - also called double)
- Boolean
- Array
- Object
- NULL
- Resource
A ___ is a sequence of characters, like “Hello world!”.
string
A ___ can be any text inside quotes. You can use single or double quotes:
string
String Functions
* Get The Length of a String
* The ___ function returns the length of a string.
PHP strlen()
- The example below returns the length of the string “Hello world!”
Example
<html>
<body>
</body>
</html>
OUPUT:
12
Count The Number of Words in a String
The ___ function counts the number of words in a string:
Example
<html>
<body>
</body>
</html>
PHP str_word_count();
OUPUT:
2
Reverse a String
* The ___ function reverses a string:
Example
<html>
<body>
</body>
</html>
PHP strrev(); OUTPUT:
!dlrow olleH
Search For a Specific Text Within a String
* The ___ function searches for a specific text within a string.
* If a match is found, the function ___ of the first match. If no match is found, it will return ___.
* The example below searches for the text “world” in the string “Hello world!”:
Example
<html>
<body>
</body>
</html>
PHP strpos();
returns the character position;
FALSE;
OUPUT:
6
Replace Text Within a String
* The ___ function replaces some characters with some other characters in a string.
* The example below replaces the text “world” with “Dolly”:
Example
<html>
<body>
</body>
</html>
PHP str_replace();
OUPUT:
Hello Dolly!
PHP Integer: An ___ is a non-decimal number between -2,147,483,648 and 2,147,483,647.
integer data type
Rules for integers:
* An integer must have (a)
* An integer must not have a (b)
* An integer can be (c)
* Integers can be specified in three formats: (d, 3)
a - at least one digit
b - decimal point
c - either positive or negative
d - decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0)
- In the following example $x is an integer. The ___ function returns the data type and value:
Example
<html>
<body>
</body>
</html>
PHP var_dump();
OUTPUT:
int(5985)
A ___ (floating point number) is a number with a decimal point or a number in exponential form.
float
In the following example $x is a float. The ___ function returns the data type and value:
Example
<html>
<body>
</body>
</html>
PHP var_dump() ;
OUTPUT:
float(10.365)
A ___ represents two possible states: TRUE or FALSE.
$x = true;
$y = false;
Boolean
___ are often used in conditional testing.
Booleans
An ___ stores multiple values in one single variable
array
An ___ is a special variable, which can hold more than one value at a time.
array
An ___ can hold many values under a single name, and you can access the
values by referring to an ___.
array; index number
Array Example
<html>
<body>
</body>
</html>
Create an Array in PHP
* In PHP, the ___ function is used to create an array
array();
In PHP, there are three types of arrays:
- Indexed arrays
- Associative arrays
- Multidimensional arrays
Arrays with a numeric index
Indexed arrays
Arrays with named keys
Associative arrays
Arrays containing one or more arrays
Multidimensional arrays
___ are “containers” for storing information.
Variables
Creating (Declaring) PHP Variables: In PHP, a variable starts with the ___ sign, followed by the ___
$ ; name of the variable
Example of variable declaration
<html>
<body>
</body>
</html>
Output:
Hello world!
5
10.5
Note: When you assign a text value to a variable, put ___ around the value.
quotes
Note: Unlike other programming languages, PHP has ___. It is created the moment you first assign a value to it.
no command for declaring a variable
Rules for PHP variables:
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
* A variable starts with the $ sign, followed by the name of the variable
* A variable name must start with a letter or the underscore character
* A variable name cannot start with a number
* A variable name can only contain alpha-numeric characters and underscores (Az, 0-9, and _ )
* Variable names are case-sensitive ($age and $AGE are two different variables)
Output Variables
* The ___ is often used to output data to the screen
PHP echo statement
In PHP, variables can be declared ___ in the script.
anywhere
The ___ is the part of the script where the variable can be referenced/used.
scope of a variable
PHP has three different variable scopes:
- local
- global
- static
A variable declared outside a function has a ___ and can only be accessed outside a function:
GLOBAL SCOPE
A variable declared within a function has a ___ and can only be accessed within that function:
LOCAL SCOPE
The ___ is used to access a global variable from within a function.
global keyword
To create a global variable, use the keyword: ___ before the variables (inside the function):
“global”
Normally, when a function is completed/executed, all of its variables are ___.
deleted
Sometimes we want a local variable NOT to be deleted.
To do this, use the keyword: “static “ when you first declare the variable:
In PHP there are two basic ways to get output:
echo and print.
echo and print are more or less the same. They are both used to ___ to
the screen.
output data
___ has no return value while ___ has a return value of 1 so it can be used in expressions.
echo; print
___ can take multiple parameters (although such usage is rare) while ___ can take one argument.
echo; print
which is marginally faster between echo and print?
echo
The echo statement can be used ___ parentheses:
with or without; echo or echo()
The print statement can be used ___ parentheses:
with or without; print or print()
An ___ is a data type which stores data and information on how to process that data.
object
- In PHP, an object must be ___.
- First we must declare a ___. For this, we use the ___. A ___ is a structure that can contain properties and methods:
explicitly declared;
class of object;
class keyword;
class
___ is a special data type which can have only one value: NULL.
Null
A variable of data type ___ is a variable that has no value assigned to it.
NULL
If a variable is created without a value, it is automatically assigned a value of ___.
NULL
Variables can also be emptied by setting the value to ___:
NULL
___ are like variables except that once they are defined they cannot be changed or undefined.
Constants
A ___ is an identifier (name) for a simple value. The value cannot be changed during the script.
constant
A ___ starts with a letter or underscore (no $ sign before the constant name).
valid constant name
Unlike variables, constants are ___ across the entire script.
automatically global
To create a constant, use the ___ function.
define()
define(name, value, case-insensitive)
Parameters:
* name: Specifies the name of the constant
* value: Specifies the value of the constant
* case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false
False or True: Constants are Global
True
___ are automatically global and can be used across the entire script.
Constants
___ are used to perform operations on variables and values.
Operators
PHP divides the operators in the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- String operators
- Array operators
The ___ are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.
PHP arithmetic operators
The ___ are used with numeric values to write a value to a variable.
PHP assignment operators
The basic assignment operator in PHP is ___. It means that the left operand gets set to the value of the assignment expression on the right
”=”
The ___ are used to combine conditional statements.
PHP logical operators
PHP has two operators that are specially designed for strings
concatenation and concatenations assignment
The ___ are used to compare arrays.
PHP array operators