Intro to PHP Flashcards
echo does what?
prints something out
what is echo format?
echo “text or content to be printed”;
what do we put at end of echo statement?
a ; so it looks like this: echo “text or content to be printed”;
what is a variable?
a variable is a container for a value - it can be declared once and then re-used.
how do we define a variable?
with a $
example of a variable?
$characterName = “joe” note text uses quotation marks
how is php executed?
in order
What would happen after line 6 and why?
“;
echo “He was $characterAge <br></br>”;
$characterName = “Mike”;
echo “He liked $characterName<br></br>”;
echo “Did not mind being $characterAge”;
?>
Name would change to Mike because this line changed the variable:
$characterName = “Mike”;
What is the difference between storing text and numbers in a variable?
text uses “”, numbers don’t need quotes
what is a string in php?
a string is plain text
a string always needs?
””
2 types of numbers as variables?
integer is a whole number - no decimal point
floating point number or floats has a decimal
A boolean
is a true or false value
Example of a boolean?
$isMale = true (can be true or false)
the 5 types of variables we just learned?
string is text with "" integer is a whole number a floating point number or floats has a decimal a boolean is true or false null has no value
a string is?
plain text
example of printing out a string
echo “my string”;
can use a variable as a container for a string?
yes for example: $phrase = “toasty toes”;
how to print the toasty toes?
echo $phrase;
a function can modify a string
yes
what’s a function to change a string to lower case?
echo strtolower($phrase);
True or False? functions are used to find out information about and to modify strings
true
what’s a function to change a string to upper case?
echo strtoupper($phrase);
what does a string function do? Can find out information about string or modify a string. They seem to start with?
str then the function - so strtolower changes to lower case
what do we have to put around the string to use a function?
() - it looks like this: echo strtolower ($phrase);
Can you use strtolower on a string that is not a variable?
How? What do we put around the string?
Yes, it would look like this:
echo strtolower (“Camel Towing”);
We put parens around the string ()
What is a function to tell use the length of a string?
strlen. So, strlen ($phrase);
What is an index and how is it called?
An index will tell us which character is in which space. This will tell us that the first character is “G” if the string is Giraffe Academy. echo $phrase[0];
How is the index numbered? 0 is 1, 1 is 2 and so on. Giraffe?
Giraffe 0=G 1=i 2=r and so on.
Also can use index function on plain strings: “Mike”
echo “Mike”[2]; will print out k
What would this do?
$phrase = “Giraffe Academy”;
$phrase[0] = “B”;
echo $phrase;
Print out the variable as “Biraffe Academy”
It changes the character in position 0 “G” to “B”
How to replace a part of a string? Command and example?
str_replace if $phrase ="Giraffe Academy"; echo str_replace ("Giraffe", "Panda", $phrase);
Another str_replace example?
if $phrase ="Cat Fish"; echo str_replace ("Cat", "Dog", $phrase); would print out Dog Fish
what does substr do?
The substr() function is used to cut a part of a string from a string, starting at a specified position.
What would this do? $phrase = "Giraffe Academy"; echo substr ($string, 8);
It would grab the string values starting a index position 8 and print it - in this case Academy
What would this do? $phrase = "Giraffe Academy"; echo substr ($string, 8, 3);
It would grab the string values starting a index position 8 and print it to 3 positions out - in this case in would print Aca
Output
3
Example
Output
Welcome to the programming world
How to print a number?
echo 40;
prints out 40
What would this print?
echo 5 + 9;
prints 14
What does this do?
echo 10 % 3;
Prints the modulus so, 10 /3 is 3, with a remainder of 1
Prints 1
echo 4 + 5 * 3;
gives us 19
but echo (4 + 5) * 3;
gives us 27 - PEMDAS order matters