cat2
cat2
<html>
<head>
<script>
function MyMessage(){
var today=new Date();
var h= today. getHours();
if(h<12) document. write(“ Good Day”);
else document.write(“ Good Bye”);
}</ script>
</ head>
<body onload=”MyMessage()”>
</ body>
</html>
PHP stands for “Hypertext Preprocessor”. It is widely used open-source scripting language primarily
designed for web development.
7.How can a PHP program determine the types of browser that a web client is using?
<?php
echo $_SERVER[‘HTTP_USER_AGENT’];
$ browser= get_Browser();
print_r($browser);
?>
8.How do you declare a variable in PHP?
In PHP, variables are declared using $ sign followed by variable name. PSP automatically determines
the variable type based on a value assign to it( dynamic typing).
example:
<?php
$var1=” hello”;//string
$var2=12;//num
$var2=12.2;//Float
?>
9. Name any two types of loops available in PHP.
1. For loop use when you know the number of iterations.
<?php
for($i=0;$i<5;$i++){
echo”the value of i is $i<br>”;
}
?>
2. while loop used when the number of hydration is unknown and its run as long as a condition is true.
<?php
$i=0;
while($i<5){
$i++;
?>
<?php
echo”Hello, World!”;
?>
PART B
Example:
2. Conditional Statements :
Conditional statements allow you to make decisions in your code based on certain conditions. The
common ones are if, else if, and else.
Example:
} else {
In JavaScript, objects are manipulated by assigning properties or methods to them or modifying their
values.
Example:
var person = {
name: "John",
age: 30
};
console.log(person.age); // Output: 34
4. Comment Statements :
Comments are non-executable statements used to add notes to the code. They are ignored by the
JavaScript engine.
Example:
/*
*/
Exception handling allows you to catch and handle errors gracefully using the try...catch block.
Example:
try {
let result = 10 / 0;
} catch (error) {
ii) Write a JavaScript program that generates a random number between 1 and 100 using the
Math object.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<p id="randomNumber"></p>
<script>
</script>
</body>
</html>
OR
// code to be executed
Function parameters are listed inside the parentheses () in the function definition.
Function arguments are the values received by the function when it is invoked.
Inside the function, the arguments (the parameters) behave as local variables.
Function Invocation
The code inside the function will execute when "something" invokes (calls) the function:
Function Return
When JavaScript reaches a return statement, the function will stop executing.
If the function was invoked from a statement, JavaScript will "return" to execute the code after the
invoking statement.
Functions often compute a return value. The return value is "returned" back to the "caller":
Example:
<!DOCTYPE html>
html>
<body>
<h1>JavaScript Functions</h1>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTMLx;
</script>
</body>
</html>
Output:
JavaScript Functions
12
Use of function:
Functions can be used the same way as you use variables, in all types of formulas, assignments, and
calculation.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Functions</h1>
<p. id-"demo"></p>
<script>
} </script>
</body>
</html>
Output:
JavaScript Functions
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
document.getElementById("colorButton").addEventListener("click", function() {
document.body.style.backgroundColor = "lightblue";
});
</script>
</body>
</html>
PHP (Hypertext Preprocessor) is a server-side scripting language designed for web development.
When a client (like a browser) requests a PHP page, here’s the process that happens:
1. Client Request: The user sends a request to the server, such as entering a URL in the browser or
submitting a form.
2. Server Processing: The web server (such as Apache or Nginx) receives the request and forwards it
to the PHP interpreter.
3. PHP Script Execution: The PHP interpreter processes the PHP code on the server. It can interact
with databases, handle files, process forms, etc.
4. HTML Output: After processing, PHP generates HTML (or other types of content) which is sent
back to the client’s browser as the response.
5. Client Display: The browser displays the HTML content, but the actual PHP code is not visible to
the client because it's executed on the server.
Example Workflow:
2. The server detects the .php extension and runs the script via the PHP engine.
4. The server sends the HTML response back to the client’s browser, which displays the result.
<?php
// This is server-side code. The client will only see the HTML output.
?>
ii) Write a program that accepts user input and display it using PHP variables.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<body>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$age = $_POST['age'];
?>
</body>
</html>
OR
12b)i) Explain the process of file uploading in PHP and demonstrate it with examples.
PHP allows you to upload single and multiple files through few lines of code only.PHP file upload
features allows you to upload binary and text files both. Moreover, you can have the full control over
the file to be uploaded through PHP authentication and file operation functions.
PHP $_FILES
The PHP global $_FILES contains all the information of file. By the help of $_FILES global, we can
get file name, file type, file size, temp file name and errors associated with file.Here, we are assuming
that file name is filename.
move_uploaded_file() function
The move_uploaded_file() function moves the uploaded file to a new location. The
move_uploaded_file() function checks internally if the file is uploaded thorough the POST request. It
moves the file if it is uploaded through the POST request.
Syntax
<?php
$target_dir = "uploads/";
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
$uploadOk = 1;
} else {
$uploadOk = 0;
if (file_exists($target_file)) {
$uploadOk = 0;
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
if ($uploadOk == 0) {
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been
uploaded.";
} else {
?>