practical - Week 5

2023-08-23

Practical 1: Do some revision

x
1:starting vs code and opening/creating folder to workwith
2:installing php server extenstion
3:creating a simple hello world in php and viewing it in browser
4:executing php in inerative shell
5:executing php code via terminal
6:creating a variable
7:types of variables in php
8:creating constants in php
9:types of constants in php
10:assigning values to variables and constants
11:displaying the types and values of variables
12:working with Arithmatic (+,-,*,/,%,++,–) operators in php
13:working with Comparison (==, !=, >, <, >=, <=) operators in php
14:working with Logical or relational (and, or, &&, !,) operators in php
15:working with Assignment Operators (=,+=,-=,*=,/=,%=) in php
16:working with Conditional or ternary(?:) Operators
17:operator categories (unary, binary, conditional, assignment)
18:Operator associativity and precedence rules
19:working with if then else
20:working with if elseif else
21:working with for loop
22:working with while loop
23:working with do while loop
24:working with switch case
25:creating arrays in php

Arrays

<?php 
//simple indexed arrays

//an empty array

$anemptyarray = array();
//or
$anotherway = [];
//or
$myarray[] = null;

var_dump($myarray);
//array with values

$myarray = array("foo","bar","bar","foo");
var_dump($myarray);

$myarray = ["foo","bar","bar","foo"];

var_dump($myarray);
//simple associative arrays with keys

$arraywithkeys = array("1"=>"a","2"=>"b","3"=>"c");
var_dump($arraywithkeys);

//or 
$array = array(
    "foo"=>"bar",
    "bar"=>"foo",
    "100"=>-100,
    -100=>100
);
var_dump($array);

//Keys not on all elements
$array = array(
    "a",
    "b",
    34=>"c",
    "d"
);
var_dump($array);

//accessing array elements with square brackets
$fruits = array("mango","banana","oranges");
var_dump($fruits[2]);

//modifying array element with square bracket syntex
$fruits[2] = "apple";

//removes the element from the array
unset($fruits[2]);

//add an element to the array
$fruits[2] = "apple";
//OR
$fruits[] = "apple";

var_dump($fruits);

//deletes the whole array
unset($fruits);


?>

Program

Write the php program that

-- creates infinite loop
-- creates an array named $studentheights
-- reads the height of 5 students in the for loop
-- add the heights to the $studentheights array
-- show the heights entered by the user using for loop