• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/28

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

28 Cards in this Set

  • Front
  • Back

What's the code for outputting 'Hello world'?

<!DOCTYPE html>
<html>
<body>

<?php
echo "Hello world!";
?>

</body>
</html>

What are the 3 ways to comment?

// single line comment



#single line comment



/* multi-line comment


if you need one */

Case sensitivity: Which of the following are ok:



<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>

Keywords aren't case-sensitive, so all would work.

Case sensitivity: Which of the following would print:



<!DOCTYPE html>
<html>
<body>

<?php
$color = "red";



echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>

</body>
</html>

Only the first one. Variables ARE case-sensitive.

In PHP, a variable starts with ______, followed by the name of the variable

a dollar sign: $



Examples:


$name = "Abby";


$age = 5

How do you declare a variable in PHP?

PHP has no command for declaring a variable. It is created the moment you first assign a value to it.

Which of these follow the rules for PHP variables:


1. $text
2. @age
3. $76trombones
4. _heythere
5. $thanks!

These are valid:


1. $text
4. _heythere



These aren't valid variables:


2. @age <-- must start with letter or underscore
3. $76trombones <-- must start with letter or underscore
5. $thanks! <-- must use only alpha-numeric characters and underscores

Will this work:



<?php
$txt = "W3Schools.com";
echo "I love $txt!";
?>

yes. outputs:



I love W3Schools.com!

Rewrite this using concatenation:



<?php
$txt = "W3Schools.com";
echo "I love $txt!";
?>


<?php
$txt = "W3Schools.com";
echo "I love " . $txt . "!";
?>

True or False:

PHP is a Loosely Typed Language


True

Talk about: PHP is a Loosely Typed Language

We do not have to tell PHP which data type the variable is.



PHP automatically converts the variable to the correct data type, depending on its value.


What is scope of a variable?




The scope of a variable is the part of the script where the variable can be referenced/used.

Name the php variable scopes.

PHP has three different variable scopes:

* local
* global
* static

A variable declared outside a function has what kind of scope?

global scope

Where can a global scope variable be accessed?

outside a function

What's the scope of $x?


<?php
$x = 5;


function myTest() {
echo "<p>Variable x inside function is: $x</p>";
}
myTest();


echo "<p>Variable x outside function is: $x</p>";
?>

global (it's declared outside of the function)

What will happen if we use $x inside the myTest() function as shown:



<?php
$x = 5;


function myTest() {
echo "<p>Variable x inside function is: $x</p>";
}
myTest();


echo "<p>Variable x outside function is: $x</p>";
?>

Output:



Error: Notice: Undefined variable: x


Variable x inside function is:


Variable x outside function is: 5



Reason: x is declared outside of the function, so is not available in the function.


What will happen when we run this:



<?php
function myTest() {
$x = 5;
echo "<p>Variable x inside function is: $x</p>";
}
myTest();



echo "<p>Variable x outside function is: $x</p>";
?>

Output:



Variable x inside function is: 5



Notice: Undefined variable: x



Variable x outside function is:

The global keyword is used to...


...access a global variable from within a function.

How do you access a global variable from within a function?

Use the global keyword before the variables (inside the function):



<?php
$x = 5;
$y = 10;


function myTest() {
global $x, $y;
$y = $x + $y;
}


myTest();
echo $y; // outputs 15
?>

PHP also stores all global variables in an array called...

$GLOBALS[index]

$GLOBALS[index]



The index holds...

..the name of the variable.

$GLOBALS[index]



True or False: This array is also accessible from within functions and can be used to update global variables directly.

true

Rewrite this using $GLOBALS[index]:



<?php
$x = 5;
$y = 10;


function myTest() {
global $x, $y;
$y = $x + $y;
}


myTest();
echo $y; // outputs 15
?>

<?php
$x = 5;
$y = 10;


function myTest() {
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}


myTest();
echo $y; // outputs 15
?>

True or False:



Normally, when a function is completed/executed, all of its variables are deleted.

True

Sometimes we want a local variable NOT to be deleted. We need it for a further job. What can we do?

use the static keyword when you first declare the variable



<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}


myTest();
myTest();
myTest();
?>

Static - What will the output be of:



<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}


myTest();
myTest();
myTest();
?>



Each time the function is called, that variable will still have the information it contained from the last time the function was called.


012

How could you get a line break at the end of each output of $x?



<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}


myTest();
myTest();
myTest();
?>

echo "<br />";



<?php
function myTest() {
static $x = 0;
echo $x;


echo "<br />";
$x++;
}


myTest();
myTest();
myTest();
?>