0% found this document useful (0 votes)
5 views

PHP 1

The document provides 4 PHP code snippets that accomplish different tasks: 1) check if a number is positive or negative, 2) display name and department in different colors, 3) display numbers 1-5 with odd numbers underlined in blue, 4) find the sum and average of 10 integers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

PHP 1

The document provides 4 PHP code snippets that accomplish different tasks: 1) check if a number is positive or negative, 2) display name and department in different colors, 3) display numbers 1-5 with odd numbers underlined in blue, 4) find the sum and average of 10 integers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Here is a PHP code snippet that accomplishes the tasks mentioned:

1. To take a number using a textbox and check whether it is positive or negative:

```php

<!DOCTYPE html>

<html>

<body>

<form method="post">

Enter a number: <input type="text" name="number">

<input type="submit" value="Check">

</form>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$number = $_POST['number'];

if ($number > 0) {

echo "$number is a positive number.";

} elseif ($number < 0) {

echo "$number is a negative number.";

} else {

echo "The number is zero.";

?>

</body>

</html>
```

2. To display your name and department in a separate line with different color:

```php

<!DOCTYPE html>

<html>

<body>

<p style="color: blue;">Your Name</p>

<p style="color: green;">Your Department</p>

</body>

</html>

```

3. To display numbers 1 to 5 with odd numbers underlined in blue color:

```php

<!DOCTYPE html>

<html>

<body>

<?php

for ($i = 1; $i <= 5; $i++) {

if ($i % 2 != 0) {

echo "<span style='color: blue; text-decoration: underline;'>$i</span> ";

} else {

echo "$i ";

}
}

?>

</body>

</html>

```

4. To find the sum and average of 10 integers:

```php

<?php

$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

$sum = array_sum($numbers);

$average = $sum / count($numbers);

echo "Sum: $sum<br>";

echo "Average: $average";

?>

```

These PHP code snippets should help you achieve the desired functionalities.

You might also like