JS Lab 3
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
<!-- Below is a header -->
|
||||
<h1>This is a header!</h1>
|
||||
|
||||
<!-- Below is a header.
|
||||
It is a very important header. -->
|
||||
<h1> This is still a header! </h1>
|
||||
|
||||
<form action="submit.php" method="post">
|
||||
<!-- Text input -->
|
||||
<label>Text</label>
|
||||
<input type="text" name="desc">
|
||||
<br><br>
|
||||
|
||||
<!-- Textarea -->
|
||||
<label>Textarea</label>
|
||||
<textarea name="desc-2"></textarea>
|
||||
<br><br>
|
||||
|
||||
<!-- Select -->
|
||||
<label>Select</label>
|
||||
<select name="food">
|
||||
<option value="burger">Burger</option>
|
||||
<option value="pizza">Pizza</option>
|
||||
<option value="pie">Pie</option>
|
||||
</select>
|
||||
<br><br>
|
||||
|
||||
<!-- Checkbox -->
|
||||
<label>Checkbox</label>
|
||||
<input type="checkbox" value="purple">
|
||||
<br><br>
|
||||
|
||||
<!-- Radio button -->
|
||||
<label>Radio</label>
|
||||
<input type="radio" value="yes">
|
||||
<br><br>
|
||||
|
||||
<!-- Button -->
|
||||
<button type="button" onclick="alert('Hello')">Alert</button>
|
||||
<br><br>
|
||||
|
||||
<!-- Example -->
|
||||
<label>Grade</label>
|
||||
<input type="text" name="grade">
|
||||
<br><br>
|
||||
|
||||
<button type="submit">Submit</button>
|
||||
|
||||
</form>
|
||||
|
||||
<?php
|
||||
|
||||
# PHP comment with hashtag.
|
||||
// PHP comment with two slashes.
|
||||
/*
|
||||
Multi-line
|
||||
PHP comment
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
// empty().
|
||||
// isset().
|
||||
// is_numeric().
|
||||
if (
|
||||
!empty($_POST['grade']) &&
|
||||
isset($_POST['grade']) &&
|
||||
is_numeric($_POST['grade'])
|
||||
) {
|
||||
$grade = $_POST['grade'];
|
||||
print_r($grade);
|
||||
}
|
||||
else {
|
||||
echo 'Grade not provided and/or was not numeric!';
|
||||
}
|
||||
|
||||
// Strip Tags.
|
||||
$text = 'Hello <b><em>world!</em></b>';
|
||||
echo $text;
|
||||
echo strip_tags($text);
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
// All of the following will give the same result.
|
||||
echo 123 + 1;
|
||||
echo '<br>';
|
||||
echo "123" + 1;
|
||||
echo '<br>';
|
||||
echo "123" + "1";
|
||||
echo '<br><br>';
|
||||
|
||||
// Arithmetic operators.
|
||||
echo '<strong>Arithmetic operators</strong><br>';
|
||||
echo 'Addition (+) ' . 1 + 2 . '<br>';
|
||||
echo 'Subtraction (-) ' . 2 - 1 . '<br>';
|
||||
echo 'Multiplication (*) ' . 2 * 2 . '<br>';
|
||||
echo 'Division (/) ' . 4 / 2 . '<br>';
|
||||
echo 'Modulus (%) ' . 4 % 2 . '<br>';
|
||||
$a = 1;
|
||||
$a++;
|
||||
echo 'Increment (++) ' . $a . '<br>';
|
||||
$a--;
|
||||
echo 'Decrement (--) ' . $a . '<br>';
|
||||
echo '<br>';
|
||||
|
||||
// Assignment operators.
|
||||
echo '<strong>Assignment operators</strong><br>';
|
||||
$a = 2;
|
||||
echo $a . '<br>';
|
||||
$a += 2;
|
||||
echo $a . '<br>';
|
||||
$a -= 2;
|
||||
echo $a . '<br>';
|
||||
$a *= 2;
|
||||
echo $a . '<br>';
|
||||
$a /= 2;
|
||||
echo $a . '<br>';
|
||||
echo '<br>';
|
||||
|
||||
// Comparison operators.
|
||||
echo '<strong>Comparison operators</strong><br>';
|
||||
$a = 1;
|
||||
$b = 2;
|
||||
$c = '2';
|
||||
|
||||
echo ($a < $b) . '<br>';
|
||||
echo ($a > $b) . '<br>';
|
||||
echo ($a == $b) . '<br>';
|
||||
echo ($a != $b) . '<br>';
|
||||
echo ($b == $c) . '<br>';
|
||||
echo ($b === $c) . '<br>';
|
||||
echo '<br>';
|
||||
|
||||
// Formatting numbers.
|
||||
echo '<strong>Formatting numbers</strong><br>';
|
||||
|
||||
echo 'Round (2.49): ' . round(2.49) . '<br>';
|
||||
echo 'Number Format (21233122):' . number_format(21233122) . '<br>';
|
||||
echo 'Printf: ';
|
||||
$sub = 5;
|
||||
$tax = 0.65;
|
||||
printf('The subtotal is %u, making the tax
|
||||
%.2f.', $sub, $tax);
|
||||
echo '<br>';
|
||||
echo 'Mt Rand: ' . mt_rand(0, 100);
|
||||
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
/* String Concatenation. */
|
||||
|
||||
// Using the dot (.) operator
|
||||
$string1 = 'Hello';
|
||||
$string2 = 'World';
|
||||
$concatenation = $string1 . $string2;
|
||||
echo $concatenation;
|
||||
echo '<br><br>';
|
||||
|
||||
// Using the concatenation assignment operator (.=)
|
||||
$string1 = 'Hello';
|
||||
$string2 = 'World';
|
||||
$string1 .= ' ' . $string2;
|
||||
echo $string1;
|
||||
echo '<br><br>';
|
||||
|
||||
// Using double quotes (") to directly concatenate variables within a string
|
||||
$string1 = 'Hello';
|
||||
$string2 = 'World';
|
||||
$concatenation = "$string1 $string2";
|
||||
echo $concatenation;
|
||||
echo '<br><br>';
|
||||
|
||||
/* Newlines */
|
||||
|
||||
// Using the "\n" escape sequence
|
||||
echo "Line 1\nLine 2\nLine 3";
|
||||
echo '<br><br>';
|
||||
|
||||
// Using double quotes (")
|
||||
echo "Line 1
|
||||
Line 2
|
||||
Line 3";
|
||||
echo '<br><br>';
|
||||
|
||||
// Using PHP_EOL constant, which represents the correct end-of-line character
|
||||
echo "Line 1" . PHP_EOL . "Line 2" . PHP_EOL . "Line 3";
|
||||
echo '<br><br>';
|
||||
|
||||
// Using the nl2br() function to insert HTML line breaks (\n) before all newlines in a string
|
||||
$text = "Line 1\nLine 2\nLine 3";
|
||||
echo nl2br($text);
|
||||
echo '<br><br>';
|
||||
|
||||
/* Handling HTML String Input */
|
||||
|
||||
// htmlspecialchars()
|
||||
$string = "<a href='test'>Test & 'Example'</a>";
|
||||
$safe_string = htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
|
||||
echo $safe_string;
|
||||
echo '<br><br>';
|
||||
|
||||
// htmlentities()
|
||||
$string = "<a href='test'>Test & 'Example'</a>";
|
||||
$safe_string = htmlentities($string, ENT_QUOTES, 'UTF-8');
|
||||
echo $safe_string;
|
||||
echo '<br><br>';
|
||||
|
||||
/* Encoding and Decoding */
|
||||
|
||||
// Encode
|
||||
$url = urlencode("https://fanshawec.ca?programs=iwd&cpa");
|
||||
echo $url;
|
||||
echo '<br><br>';
|
||||
|
||||
// Decode
|
||||
$url = urldecode("https%3A%2F%2Ffanshawec.ca%3Fprograms%3Diwd%26cpa");
|
||||
echo $url;
|
||||
echo '<br><br>';
|
||||
|
||||
/* Adjusting strings */
|
||||
|
||||
// substr()
|
||||
$string = "You are a wizard, Harry.";
|
||||
$substr = substr($string, 9, 6);
|
||||
echo $substr;
|
||||
echo '<br><br>';
|
||||
|
||||
// strtok()
|
||||
$string = "Luke, I am your father.";
|
||||
$substr = strtok($string, ",");
|
||||
echo $substr;
|
||||
echo '<br><br>';
|
||||
|
||||
// strlen()
|
||||
$string = "May the odds be ever in your favour.";
|
||||
$length = strlen($string);
|
||||
echo $length;
|
||||
echo '<br><br>';
|
||||
|
||||
// Uppercase / Lowercase
|
||||
$string = "hello world";
|
||||
echo ucfirst($string);
|
||||
echo '<br><br>';
|
||||
echo ucwords($string);
|
||||
echo '<br><br>';
|
||||
echo strtoupper($string);
|
||||
echo '<br><br>';
|
||||
echo strtolower('HELLO WORLD');
|
||||
echo '<br><br>';
|
||||
|
||||
/* Replacing Strings */
|
||||
|
||||
// str_replace()
|
||||
$search = 'world';
|
||||
$replace = 'everyone';
|
||||
$subject = 'Hello world!';
|
||||
echo str_replace($search, $replace, $subject);
|
||||
echo '<br><br>';
|
||||
|
||||
// str_ireplace()
|
||||
$search = 'WORLD';
|
||||
$replace = 'everyone';
|
||||
$subject = 'Hello world!';
|
||||
echo str_ireplace($search, $replace, $subject);
|
||||
echo '<br><br>';
|
||||
|
||||
// trim()
|
||||
$string = " hello world ";
|
||||
echo trim($string);
|
||||
echo '<br><br>';
|
||||
|
||||
// rtrim()
|
||||
$string = "hello world!!!";
|
||||
echo rtrim($string, '!');
|
||||
echo '<br><br>';
|
||||
|
||||
$string = " hello world";
|
||||
echo ltrim($string);
|
||||
echo '<br><br>';
|
||||
|
||||
@@ -0,0 +1,201 @@
|
||||
<?php
|
||||
|
||||
// -----------------------------
|
||||
// IF statement
|
||||
// -----------------------------
|
||||
|
||||
if ($condition) {
|
||||
// code.
|
||||
}
|
||||
|
||||
// E.g.
|
||||
if ($year === 2024) {
|
||||
$isOlympicYear = TRUE;
|
||||
}
|
||||
|
||||
// Bonus: IF statements with a lot of conditions
|
||||
if (
|
||||
$condition_1 &&
|
||||
(
|
||||
$condition_2 ||
|
||||
$condition_3
|
||||
) &&
|
||||
$condition_4
|
||||
) {
|
||||
// code.
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// IF/ELSE statement
|
||||
// -----------------------------
|
||||
|
||||
if ($condition) {
|
||||
// code.
|
||||
}
|
||||
else {
|
||||
// other code.
|
||||
}
|
||||
|
||||
// E.g.
|
||||
if ($year === 2024) {
|
||||
$isOlympicYear = TRUE;
|
||||
}
|
||||
else {
|
||||
$isOlympicYear = FALSE;
|
||||
}
|
||||
|
||||
// Alternatively, you could write the above like this.
|
||||
$isOlympicYear = FALSE;
|
||||
if ($year === 2024) {
|
||||
$isOlympicYear = TRUE;
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// ELSEIF statements.
|
||||
// -----------------------------
|
||||
|
||||
if ($condition_1) {
|
||||
// code.
|
||||
}
|
||||
elseif ($condition_2) {
|
||||
// other code.
|
||||
}
|
||||
else {
|
||||
// other other code.
|
||||
}
|
||||
|
||||
// E.g.
|
||||
if ($year === 2024) {
|
||||
$isOlympicYear = TRUE;
|
||||
}
|
||||
elseif ($year === 2025) {
|
||||
$isOlympicYear = FALSE;
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Nesting statements
|
||||
// -----------------------------
|
||||
|
||||
// E.g.
|
||||
if ($year === 2024) {
|
||||
$isOlympicYear = TRUE;
|
||||
|
||||
if ($city === 'Paris') {
|
||||
echo "Bonjour!";
|
||||
}
|
||||
else {
|
||||
echo "Hello!";
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Early Return statements
|
||||
// -----------------------------
|
||||
|
||||
function checkAge($age) {
|
||||
// Early return if age is less than 18
|
||||
if ($age < 18) {
|
||||
return "Too young";
|
||||
}
|
||||
// Continue with the function if age is 18+
|
||||
return "Welcome!";
|
||||
}
|
||||
|
||||
echo checkAge(16); // Outputs: Too young
|
||||
echo checkAge(20); // Outputs: Welcome!
|
||||
|
||||
// -----------------------------
|
||||
// Ternary Expressions
|
||||
// - Only good for replacing non-complex IF/ELSE statements
|
||||
// -----------------------------
|
||||
|
||||
$age = 20;
|
||||
$can_vote = ($age >= 18) ? 'Yes' : 'No'
|
||||
|
||||
// Alternatively, you could write the above like this, but the above saves lines of code.
|
||||
if ($age >= 18) {
|
||||
$can_vote = 'Yes';
|
||||
}
|
||||
else {
|
||||
$can_vote = 'No';
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Switch statements
|
||||
// -----------------------------
|
||||
|
||||
// E.g.
|
||||
switch ($isOlympicYear) {
|
||||
case 2023:
|
||||
break; // Without a break, the code will continue executing to the next case.
|
||||
|
||||
case 2024:
|
||||
break;
|
||||
|
||||
case 2025:
|
||||
break;
|
||||
|
||||
default: // Default is the equivalent to an ELSE
|
||||
break;
|
||||
}
|
||||
|
||||
// Using a switch like above instead of the following
|
||||
if ($year === 2023) {
|
||||
//
|
||||
}
|
||||
elseif ($year === 2024) {
|
||||
//
|
||||
}
|
||||
elseif ($year === 2025) {
|
||||
//
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// For loops
|
||||
// -----------------------------
|
||||
|
||||
// E.g.
|
||||
for ($i = 1; $i < 5; $i++) {
|
||||
echo "This is the $i -th iteration.";
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// While loops
|
||||
// -----------------------------
|
||||
|
||||
// E.g.
|
||||
$i = 1;
|
||||
while ($i < 5) {
|
||||
echo "This is the $i -th iteration.";
|
||||
$i++;
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Passing Arguments by Reference
|
||||
// -----------------------------
|
||||
|
||||
function add_five(&$value) {
|
||||
$value += 5;
|
||||
}
|
||||
|
||||
$num = 2;
|
||||
add_five($num);
|
||||
echo $num; // Outputs: 7
|
||||
|
||||
// -----------------------------
|
||||
// Using the ... operator
|
||||
// - Allows for any number of arguments
|
||||
// -----------------------------
|
||||
|
||||
function sum(...$numbers) {
|
||||
$sum = 0;
|
||||
|
||||
for ($i = 0; $i < count($numbers); $i++) {
|
||||
$sum += $numbers[$i];
|
||||
}
|
||||
|
||||
return $sum;
|
||||
}
|
||||
|
||||
echo sum(1, 2, 3, 4); // Output: 10
|
||||
|
||||
@@ -0,0 +1,194 @@
|
||||
<?php
|
||||
|
||||
/* Arrays */
|
||||
|
||||
$months = array(
|
||||
'January', // Index = 0
|
||||
'February', // Index = 1
|
||||
'March', // Index = 2
|
||||
);
|
||||
echo '<pre>';
|
||||
print_r($months);
|
||||
echo '</pre>';
|
||||
|
||||
// Getting value from index
|
||||
echo '<pre>';
|
||||
print_r($months[1]);
|
||||
echo '</pre>';
|
||||
|
||||
// Short hand
|
||||
$months = [
|
||||
'January', // Index = 0
|
||||
'February', // Index = 1
|
||||
'March', // Index = 2
|
||||
];
|
||||
|
||||
/* Associative Arrays */
|
||||
|
||||
$months = [
|
||||
'month1' => 'January',
|
||||
'month2' => 'February',
|
||||
'month3' => 'March'
|
||||
];
|
||||
echo '<pre>';
|
||||
print_r($months);
|
||||
echo '</pre>';
|
||||
echo '<pre>';
|
||||
print_r($months['month1']);
|
||||
echo '</pre>';
|
||||
|
||||
/* Working with Arrays */
|
||||
|
||||
// range()
|
||||
$numbers = range(1, 10); // Same as $numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||
echo '<pre>';
|
||||
print_r($numbers);
|
||||
echo '</pre>';
|
||||
|
||||
$numbers = range(1, 10, 2); // Same as $numbers = [1, 3, 5, 7, 9];
|
||||
echo '<pre>';
|
||||
print_r($numbers);
|
||||
echo '</pre>';
|
||||
|
||||
// Adding to arrays
|
||||
$months = [
|
||||
'January', // Index = 0
|
||||
'February', // Index = 1
|
||||
'March', // Index = 2
|
||||
];
|
||||
// Add to index 3
|
||||
$months[3] = 'April';
|
||||
// Change index 2
|
||||
$months[2] = 'May';
|
||||
// Add to end of array
|
||||
$months[] = 'June';
|
||||
echo '<pre>';
|
||||
print_r($months);
|
||||
echo '</pre>';
|
||||
|
||||
/* Merging Arrays */
|
||||
$sem1grades = [
|
||||
'math' => 90,
|
||||
'chemistry' => 67,
|
||||
'biology' => 88,
|
||||
'history' => 45,
|
||||
'gym' => 60,
|
||||
];
|
||||
$sem2grades = [
|
||||
'philosophy' => 87,
|
||||
'art' => 99,
|
||||
'physics' => 71,
|
||||
'gym' => 60,
|
||||
];
|
||||
|
||||
// array_merge()
|
||||
$allGrades = array_merge($sem1grades, $sem2grades);
|
||||
echo '<pre>';
|
||||
print_r($allGrades);
|
||||
echo '</pre>';
|
||||
|
||||
// Array Union
|
||||
$allGrades = $sem1grades + $sem2grades;
|
||||
echo '<pre>';
|
||||
print_r($allGrades);
|
||||
echo '</pre>';
|
||||
|
||||
/* Multidimensional arrays */
|
||||
$meats = [
|
||||
'Pepperoni',
|
||||
'Bacon',
|
||||
'Ham',
|
||||
];
|
||||
$cheeses = [
|
||||
'Mozzarella',
|
||||
'Cheddar',
|
||||
'Swiss',
|
||||
];
|
||||
$veggies = [
|
||||
'Onions',
|
||||
'Peppers',
|
||||
'Mushrooms',
|
||||
];
|
||||
$toppings = [
|
||||
'Meats' => $meats,
|
||||
'Cheeses' => $cheeses,
|
||||
'Veggies' => $veggies,
|
||||
'Size' => 'Medium',
|
||||
];
|
||||
echo $toppings['Veggies'][1];
|
||||
echo '<br><br>';
|
||||
|
||||
/* Printing Arrays */
|
||||
|
||||
// foreach()
|
||||
foreach ($toppings as $type => $value) {
|
||||
echo "{$type}:</br>";
|
||||
if (is_array($value)) {
|
||||
foreach ($value as $number => $topping) {
|
||||
echo "Topping #{$number} is $topping</br>";
|
||||
}
|
||||
}
|
||||
else {
|
||||
echo "{$type} is $value</br>";
|
||||
}
|
||||
echo "</br>";
|
||||
}
|
||||
|
||||
$words = [
|
||||
'The',
|
||||
'cow',
|
||||
'jumped',
|
||||
'over',
|
||||
'the',
|
||||
'moon',
|
||||
];
|
||||
// implode()
|
||||
$sentence = implode(' ', $words);
|
||||
echo $sentence;
|
||||
echo '<br><br>';
|
||||
// explode()
|
||||
$words2 = explode(' ', $sentence);
|
||||
echo $words2;
|
||||
echo '<br><br>';
|
||||
|
||||
/* Sorting Arrays */
|
||||
|
||||
$array1 = [
|
||||
3,
|
||||
1,
|
||||
4,
|
||||
1,
|
||||
5,
|
||||
9,
|
||||
];
|
||||
// sort()
|
||||
sort($array1);
|
||||
echo '<pre>';
|
||||
print_r($array1);
|
||||
echo '</pre>';
|
||||
// rsort()
|
||||
rsort($array1);
|
||||
echo '<pre>';
|
||||
print_r($array1);
|
||||
echo '</pre>';
|
||||
|
||||
$assocArray = [
|
||||
'a' => 3,
|
||||
'b' => 1,
|
||||
'c' => 4,
|
||||
];
|
||||
// asort()
|
||||
asort($assocArray);
|
||||
echo '<pre>';
|
||||
print_r($assocArray);
|
||||
echo '</pre>';
|
||||
$assocArray = [
|
||||
'a' => 3,
|
||||
'b' => 1,
|
||||
'c' => 4,
|
||||
];
|
||||
// ksort()
|
||||
ksort($assocArray);
|
||||
echo '<pre>';
|
||||
print_r($assocArray);
|
||||
echo '</pre>';
|
||||
Reference in New Issue
Block a user