Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialYu-Che Hung
Courses Plus Student 10,607 Pointsdon't know this answer &why
don't know this answer &why
3 Answers
andrewgabriel
18,106 Points$POST variables are used in order to encrypt data from the front-end user to see therefore when a page is refreshed the values are not sent to the new page. In most common cases, the POST values are going to a database or a file to check something or extract data based on these values entered.
On the other hand $GET variables server the same purpose however they DO get sent to the refreshed page and if you want to try this in action, try setting up a form with method set to get then fill out the form and submit it. You will see on the refreshed page in the URL things like ?name=andrew?email=a@a.com etc... and that's the main the different.
I hope that was a clear enough explanation, if you want to read more about this, I suggest you take a look at https://www.w3schools.com/tags/ref_httpmethods.asp
andrewgabriel
18,106 PointsYen Wei Liu The code you posted will print Hello World! to the screen then immediately go to the page named newfile.php.
Using the header("Location: anyfilename.php"); is a very powerful function in PHP that is used to redirect users from one page to another. To put it in a practical example, you might be developing a login page where users enter some values in the email and password field then after they hit submit and you verify their information, you will need to use this function to redirect them to their profile page or the homepage inside your web application.
It can also be used for other instances like redirecting users to a thank you page after they fill out and submit a contact form.
Yu-Che Hung
Courses Plus Student 10,607 PointsBut the answer is A: Hello World! and a Warning that's why I don't get it. Originally, I was thinking about the same as you posted. But the answer just made me confused.
andrewgabriel
18,106 PointsYu-Che Hung I see what you mean now. I just tested the code through http://phptester.net and there is indeed a warning that prints after Hello World!
After doing some research I found out that you can't redirect the page after already printing something to the user or in other words sending a response. I recommend you check out this thread on Stackoverflow, it goes in more detail about other ways you can make this work. http://stackoverflow.com/questions/43171753/how-to-redirect-to-another-page-in-php-using-header-after-outputting-message
Great question btw!
Yu-Che Hung
Courses Plus Student 10,607 Pointsthanks! you're so patient with my questions! appreciate for that!
Yu-Che Hung
Courses Plus Student 10,607 PointsYu-Che Hung
Courses Plus Student 10,607 PointsWhat does the following code return?
<?php
echo "Hello World!";
header("Location: newfile.php");
Yu-Che Hung
Courses Plus Student 10,607 PointsYu-Che Hung
Courses Plus Student 10,607 Pointshi andrew : I forgot to paste the question THAT I don't know