r/PHPhelp • u/EstimateCapable6531 • Sep 16 '25
Need help asap in Php
- Write a PHP script that takes three exam scores as input and calculates the average score using arithmetic operations ((score1 + score2 + score3) / 3).
- Extend your script to calculate and display the percentage score based on a total of 300 marks (assuming each of the 3 exams is out of 100).
- Take as input marks for five subjects. Count how many subjects have a score below 50 (fail). If a student fails in more than two subjects, display a warning message: "Student is placed on academic probation."
This is the question. I am new to PHP, and it's just week 1. The problem is that after inputting values, the arithmetic operations don't perform and don't give any output. It doesn't give any error either. Just no output except taking input values. I've tried: changing the php block position, changing if ($_SERVER["REQUEST_METHOD"] == "POST") to !empty POST condition. nothing works. On other PHP compiler platforms, this code doesn't even produce input sections; it just says executed successfully and then gives me the same code which I have written in the <form> section. Need help.
Here is my code:
This only displays input boxes and takes input, but no output after submitting input values. I am using W3Schools PHP.
I don't know why my code is displaying like this. I tried editing my post many times, but it is still like that. if you can tell me how to insert code here properly, then I will repost it so I can receive guidelines on my code.
Many thanks in advance.
<?php if ($_SERVER\["REQUEST_METHOD"\] == "POST") { // --- Part 1: Average of 3 exams --- $exam1 = (int) $_POST\['exam1'\]; $exam2 = (int) $_POST\['exam2'\]; $exam3 = (int) $_POST\['exam3'\]; $average = ($exam1 + $exam2 + $exam3) / 3; echo "<h4>Average of 3 exams: $average</h4>"; // --- Part 2: Percentage (out of 300) --- $total3 = $exam1 + $exam2 +$exam3; $percentage = ($total3 / 300) \* 100; echo "<h4>Percentage (3 exams out of 300): $percentage%</h4>"; // --- Part 3: 5 subject fail count --- $subjects = \[ (int) $_POST\['subj1'\], (int) $_POST\['subj2'\], (int) $_POST\['subj3'\], (int) $_POST\['subj4'\], (int) $_POST\['subj5'\] \]; $failCount = 0; foreach ($subjects as $score) { if ($score < 50) { $failCount++; } } echo "<h4>Number of failed subjects: $failCount</h4>"; if ($failCount > 2) { echo "<strong style='color:red;'>Student is placed on academic probation.</strong>"; } } ?>
<form method="post">
<h3>Enter 3 exam scores (for average & percentage):</h3>
Exam 1: <input type="number" name="exam1"><br><br>
Exam 2: <input type="number" name="exam2"><br><br>
Exam 3: <input type="number" name="exam3"><br><br>
<h3>Enter 5 subject scores (for fail count):</h3>
Subject 1: <input type="number" name="subj1"><br><br>
Subject 2: <input type="number" name="subj2"><br><br>
Subject 3: <input type="number" name="subj3"><br><br>
Subject 4: <input type="number" name="subj4"><br><br>
Subject 5: <input type="number" name="subj5"><br><br>
<input type="submit" value="Submit">
</form>
4
u/MateusAzevedo Sep 16 '25
Read the rule #1 on the right side. Personally, I prefer if you use Gist.
As a quick start, what happens if you add this:
Do you see anything on screen after submitting?
To learn more about basic debugging: https://phpdelusions.net/basic_principles_of_web_programming#debugging
3
u/Big-Dragonfly-3700 Sep 16 '25
What is the file extension you are using? Are you requesting the code using a URL to a web server that has php installed on it, such as http://localhost/your_file.php ? What does the 'view source' of the page show in your browser?
2
u/keyboarddevil Sep 16 '25
There appears to be lot of unnecessary escapes in your code "\".
1
u/equilni Sep 17 '25
OP's code formatted for anyone else:
Now to your question:
a)
I am new to PHP, and it's just week 1Easiest solution is to think smaller and break each segment into workable sections.
$average = ($exam1 + $exam2 + $exam3) / 3;- if you change the exam variables to actual numbers andecho $averagedoes that match what you are looking for? If yes, move the next part.b)
On other PHP compiler platformsNo idea what this mean, but looking at comments you are talking about W3Schools Try it. Using these tools (like onlinephp.io) means you need to modify the code for running there once - ie going back to my first point of breaking this down to smaller segments.
ie:
Install PHP only and run the development server. Week 1, you really don't need anything else right now.
https://www.php.net/manual/en/features.commandline.webserver.php
c) Test the form separately. See how I reduced the code to smaller segments? Do the same with the form.
d) Now slowly start merging things back and test.
0
u/alliejim98 Sep 16 '25
Do those escape characters (
\\) actually exist in your PHP file, or did they just show up when you pasted the code into Reddit? If they’re in your real file, that’s the main reason it won’t run — you’ll need to remove them. Also, watch out for your use of single line (//) comments. When you put code on the same line after a//, everything after it gets ignored by PHP. Best practice is to keep your comments on their own lines and break your code into multiple lines so it’s easier to read and debug.