Jump to content

Understanding PHP actions

Sergio45
Go to solution Solved by LyyK,
3 hours ago, Sergio45 said:

So does the case statements have an order that there executed or are they executed the moment that there is a match of the case. I get that 


 case "firstPage":
        include_once("Views/firstPage.php");
        break;

Makes it show the firstPage.php when the action which is the name of the variable and firstPage which  is what is assigned to that variable (action).

In the order of precedence. The block of the first matching case is run and no other case of the switch should be evaluated afterward if there's a break in the block.

 

Quote

Short version:I don't see the action firstPage being declared anywhere other than here:


$_REQUEST['action'] = (isset($_REQUEST['action']) ?$_REQUEST['action'] : "firstPage");

Which is in the Index.php: I know that Index is the first thing that is loaded.

So does the above code start off assigned with the string value of "firstPage" the moment it is loaded....Right? 

As long as $_REQUEST['action'] has not been set — which is, in your project's case (no pun intended), always — it will have "firstPage" assigned to it. The reason the "firstPage" case didn't run before was simply because of the lowercase 'p' :)

Quote

So that is why we don't have to declare the action "firstPage" anywhere in the firstPage.php file because it is declared right of the bat and matches the case statement and loads the firstPage.php...Right?

 

Correct. Have you learned about compile vs runtime yet? At runtime, the code in firstPage.php is effectively considered as code within whatever file it was included by. I think what the assignment is trying to show with the header and footer is that instead of having to have the code from the header/footer files in each file that require it, you can simply include it instead. This makes for editing of code that is used by multiple files quicker and much more efficient.

 

If you ever have any questions about coding related topics, you're very welcome to message me and I'll link you my discord or Steam etc. I'm by no means the best web developer out there but I'm always happy to help with this sort of stuff :)

2 hours ago, Sergio45 said:

I will be back at it again tomorrow. I have to get ready for bed for work. 

Thanks for the help.

LyyK

Appreciate it man.

 

I fixed the problems and added comments to the changed files to explain what was wrong :) Sorry for taking so long; I can't compile PHP on my winbox and I'm not much of a web developer haha. After unzipping the files into my local server, it only took a couple of minutes to figure out. Let me know if everything checks out once you have had a chance to look it over.

 

As for why you were getting "Unexpected action  found.<br>": in short, the casing of the action string didn't match any of the switch cases (firstpage != firstPage) so it would always run the default case below

echo "Unexpected action $action found.<br>";

 

As you may have also noticed, the message you were getting contained two spaces in it because $action no longer contained a value after you changed it to assign the value to $_REQUEST['action'] instead.

 

I changed the statement in the default case to the statement below and now, when the default case is run, the stored value of the string will output properly.

echo "Unexpected action " . $_REQUEST['action'] . " found.<br>";

 

This would also work

// Assigning the value of the superglobal to a string variable 
$action = $_REQUEST['action'];
echo "Unexpected action $action found.<br>";

As would this

// Or simply echo the superglobal var like shown in the statement below.
// Note, the variable name does not require quotations of its own when it is already encapsulated within a string.
echo "Unexpected action $_REQUEST[action] found.<br>"; 

 

These would not

// Escaping the quotation characters ("\"" or "\'"), like shown below, does not work and will throw a 500 internal server error
echo "Unexpected action $_REQUEST[\'action\'] found.<br>";
echo "Unexpected action $_REQUEST[\"action\"] found.<br>";

 

Quote

With that being said. How the heck is firstPage action being assigned to the firstpage if its not in any of the values and actions on the firstPage.php

I somehow missed this question. The file firstPage.php contain nothing but two include statements; one for header.php and another for footer.php. The "action" is only just the name of a variable and "firstPage" is its string value assigned by a statement in your index.php file. Your switch statement evaluates the string stored in the action variable and executes any case block with a matching string value. Naturally, if none of the cases match, the default case runs. The firstPage.php file is "run" by the include statement within the "firstPage" case block. Essentially, when the firstPage case block is run, the content of the header and footer files are effectively run by the index (think inheritance). The header and footer contain HTML which is loaded into the current HTML Document.

 

If you want to know more about variables/values with a "global" scope, read up on PHP Sessions. Unlike global variables, session variables are only for the current user and they die once the user session is terminated or times out. Just be careful with how you use them as, depending on who your professor is, you might get deducted on an assignment for using them improperly (e.g. using session vars when you could just as well have passed the value as a parameter to wherever it is to be used.

 

Cheers!

 

week6.zip

[ Intel i7-6700K ][ Asus TUF Sabertooth Mk1 Z170 ][ Gigabyte GTX 780ti ][ 32GB (2x16GB) 3200MHz G.Skill Trident Z ][ Corsair Obsidian 900D ]

[ EVGA SuperNOVA 1000 P2 80+ PLATINUM ][ 1x512GB Samsung 960 Pro M.2 (OS) ][ 3x512GB Samsung 850 Pro RAID 0 ][ 1x5TB WD Black ]

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, LyyK said:

Your switch statement evaluates the string stored in the action variable and executes any case block with a matching string value. Naturally, if none of the cases match, the default case runs. The firstPage.php file is "run" by the include statement within the "firstPage" case block.

So does the case statements have an order that there executed or are they executed the moment that there is a match of the case. I get that 

 case "firstPage":
        include_once("Views/firstPage.php");
        break;

Makes it show the firstPage.php when the action which is the name of the variable and firstPage which  is what is assigned to that variable (action).

 

Short version:I don't see the action firstPage being declared anywhere other than here:

$_REQUEST['action'] = (isset($_REQUEST['action']) ?$_REQUEST['action'] : "firstPage");

Which is in the Index.php: I know that Index is the first thing that is loaded.

So does the above code start off assigned with the string value of "firstPage" the moment it is loaded....Right? 

So that is why we don't have to declare the action "firstPage" anywhere in the firstPage.php file because it is declared right of the bat and matches the case statement and loads the firstPage.php...Right?

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Sergio45 said:

So does the case statements have an order that there executed or are they executed the moment that there is a match of the case. I get that 


 case "firstPage":
        include_once("Views/firstPage.php");
        break;

Makes it show the firstPage.php when the action which is the name of the variable and firstPage which  is what is assigned to that variable (action).

In the order of precedence. The block of the first matching case is run and no other case of the switch should be evaluated afterward if there's a break in the block.

 

Quote

Short version:I don't see the action firstPage being declared anywhere other than here:


$_REQUEST['action'] = (isset($_REQUEST['action']) ?$_REQUEST['action'] : "firstPage");

Which is in the Index.php: I know that Index is the first thing that is loaded.

So does the above code start off assigned with the string value of "firstPage" the moment it is loaded....Right? 

As long as $_REQUEST['action'] has not been set — which is, in your project's case (no pun intended), always — it will have "firstPage" assigned to it. The reason the "firstPage" case didn't run before was simply because of the lowercase 'p' :)

Quote

So that is why we don't have to declare the action "firstPage" anywhere in the firstPage.php file because it is declared right of the bat and matches the case statement and loads the firstPage.php...Right?

 

Correct. Have you learned about compile vs runtime yet? At runtime, the code in firstPage.php is effectively considered as code within whatever file it was included by. I think what the assignment is trying to show with the header and footer is that instead of having to have the code from the header/footer files in each file that require it, you can simply include it instead. This makes for editing of code that is used by multiple files quicker and much more efficient.

 

If you ever have any questions about coding related topics, you're very welcome to message me and I'll link you my discord or Steam etc. I'm by no means the best web developer out there but I'm always happy to help with this sort of stuff :)

[ Intel i7-6700K ][ Asus TUF Sabertooth Mk1 Z170 ][ Gigabyte GTX 780ti ][ 32GB (2x16GB) 3200MHz G.Skill Trident Z ][ Corsair Obsidian 900D ]

[ EVGA SuperNOVA 1000 P2 80+ PLATINUM ][ 1x512GB Samsung 960 Pro M.2 (OS) ][ 3x512GB Samsung 850 Pro RAID 0 ][ 1x5TB WD Black ]

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×