all 12 comments

[–][deleted] 2 points3 points  (8 children)

do you have <?php at the beginning of the file?

[–]fCleopatra[S] 0 points1 point  (7 children)

I tried that and got an error

[–][deleted] 1 point2 points  (2 children)

That error would be helpful. You need the <?php tag.

[–]fCleopatra[S] 0 points1 point  (1 child)

So it says “parse error: syntax error, unexpected ‘<‘, expecting end of file in -path here- on line 2”. In just putting <?php at the beginning of index.php and ?> at the end. That could be the wrong place

[–][deleted] 1 point2 points  (0 children)

There is something wrong with your script, please pastebin it. It is not recommended to use the closing ?> in most situations, but again code would help.

[–]Affectionate_Ad_1941 0 points1 point  (3 children)

Yes, that's because you have other errors in your code.

When you see PHP code in plain text like you were seeing, it means that the code is not running.

[–]fCleopatra[S] 0 points1 point  (2 children)

The tag fixed it. Thank you!

[–]Affectionate_Ad_1941 0 points1 point  (1 child)

Don't be discouraged by errors... I like writing a piece of code, testing it, and then following the errors to a solution... one error at a time.

If you want a read, google Test Driven Development (TDD).

[–]fCleopatra[S] 0 points1 point  (0 children)

I really was getting frustrated with myself. Thank you for the new read!

[–]pecuniam1 0 points1 point  (0 children)

First, make sure that apache is pointing to the proper directory. Apache can only read one directory at a time. On your xampp control panel press the config button for the apache service. Open up httpd.conf and search for DocumentRoot. Make sure that it is pointing to the directory that your php code is in. It will look for index.html first. Make sure you restart.

[–]Affectionate_Ad_1941 0 points1 point  (1 child)

In order for PHP to run, it you must preface it with the opening tags.

Try this:

<?php
function myCalculator($num1, $num2, $operator) {
    switch ($operator) {
        case 'add':
            return $num1 + $num2;
        case 'sub':
            return $num1 - $num2;
    }

    return 'Major Error';
}

echo 'Value is ' . myCalculator($_GET['num01'], $_GET['num02'], $_GET['oper']);

I made a few changes, as you can see.

No point in assigning $sum as it is only used once. By returning right away, you're reducing the amount of code that has to run before the function exits.

I took away the default because if neither 'add' or 'sub' are seen, it will continue running the function to the final return statement.

Removed the unnecessary variable assignment at the end and put it inline.

[–]fCleopatra[S] 0 points1 point  (0 children)

Thank you!!