Given a beginner PHP assignment, not entirely sure what this means - php

http://localhost:8888/test.php
I typed in the above URL to navigate to a blank php file I'd created, and my friend typed the following in my URL bar:
?one=3&two=18
(after /test.php)
He said that when I refresh the page, he wants to see the number 21 show up. I'm not entirely sure where to start. Any help for this PHP beginner is appreciated.
Thanks!

Try this...
<?php
if (isset($_GET['one']) AND isset($_GET['two'])) {
$one = (int) $_GET['one'];
$two = (int) $_GET['two'];
echo $one + $two;
}

? is the operator used to indicate the start of the parameters passed to a webpage. You can pass many of them separated by the character &. Based on that you need your website to get those parameters and perform the only operation that will get you the result 21 from 18 and 3.
So you need your webpage to display the sum of your first parameter and your second parameter.
You can get those by using $_GET.
Bottom line what you need is:
Learn how to get the parameters passed
Learn how to do operations with them (sum)
Learn how to display the result.
Learn about UrlEncode
Good luck on the php world!

to obtain something from your query string (?... part of the URL) you have to use $_GET[] i.e.
$one = $_GET['one'];
$two = $_GET['two'];
echo $one+$two; //print it

You need to GET the variables from that URL and manipulate them.
http://www.w3schools.com/php/php_get.asp
But you first and foremost need to learn from the ground up. I suggest this tutorial:
http://www.tizag.com/phpT/

<?php
echo $_GET['one'] + $_GET['two'];
?>

print_r($_GET) will show you the anwser.

Related

Ampersand doesn't load in Query String - PHP

I'm passing simple query string:
index.php?a=orange&apple
Getting value with:
$_GET['a'];
it only shows orange value. i have tried
$a = urlencode($_GET['a']);
$rs = urldecode($a);
echo $rs; // orange;
but didn't work. Found similar question here stackoverflow but seems not useful. How do i get complete value orange&apple?
That is because & makes apple as another value in GET request.
Like this -
/test/demo_form.php?name1=value1&name2=value2
So use '%26'(encoding URI Component) in place of & like this -
index.php?a=orange%26apple
And get the value with $_GET['a'];. This should work.
Your query string need to encode ampersand(&) as percent-encode as %26. Your url will be like :
index.php?a=orange%26apple
Write it like this:
index.php?a=orange&b=apple
// Values
$_GET['a'];
$_GET['b'];

How to get first part of a URL

can anyone help me to piece together the puzzled I'm facing. Lets say I have url's
/some-work/
/store/bread/alloy/
and in both of these cases I wanna fetch the first part from it. i.e. some-work, store.
Now I've used parse_url(get_permalink()) to get the array of the url and then fetch the path index of the array to fetch the above string. Now I have also checked strstr PHP function, but I am unable to make it work. Can anyone help?
You can use explode, array_filter and current function like as
$url = "http://www.example.com/some-work/";
$extracted = array_filter(explode("/",parse_url($url,PHP_URL_PATH)));
echo current($extracted);//some-work
Demo

play with PHP GET

can anyone help me to play with GET urls for example I have a link like this:
?id=5&lang=1
So my question is how can I make this one:
?id=5,1
I don't want to show the &lang, only I want is that the &lang to replace with , "comma" can anyone help me?
You can use mod_rewrite to rewrite ?id=5,1 to ?id=5&lang=1 internally.
Otherwise, the value of id will be 5,1. Your application would then need to know that id contains more than the id. It could then parse out the language from the id. However, this will become confusing when you introduce more parameters.
Assuming you have already built the URL in the way you have specified, you can break the id field based on the comma and extract the real id and lang field
$urlPieces = explode(",", $_GET['id']);
$id = $urlPieces[0];
$lang = $urlPieces[1];
You are able to do this, but it's not very clean, in terms of the proper $_GET variable values. The solution automatically type casts the values to integers:
sscanf($_GET['id'], '%d,%d', $id, $lang);
// $id = int(5)
// $lang = int(1)
Two solutions:
Firstly, you could simply reformat the parameters when they arrive in your PHP program. With ?id=5,1, you'll get a PHP $_GET array with id '5,1'. This you can simply split using the explode() function to get the two values you want.
The second solution is to use the Apache mod_rewrite feature, to modify the URL arguments before they arrive at PHP. For this, you'll need to understand regular expressions (regex), as mod_rewrite uses this for it's work. You should google 'mod_rewrite' and 'regex' to find out more.
However mod_rewrite is typically used to get rid of GET arguments entirely. For example the URLs of the questions on this site do not have any get arguments, but the server translates the arguments between the slashes into GET arguments. This is considered better practice than simply than changing how the arguments look, as it is more user-friendly and SEO friendly.
Hope that helps.
$id = $id . ',' . $lang;
<a href="?<?php echo $id; ?>">

Best way to add/change 1 GET value while keeping others?

How can I make a link that justs adds or changes 1 GET var while maintaining all others?
I have a page that is created using different GET vars.
So it will be like mypage.php?color=red&size=7&brand=some%20brand
So I want to have a link that sets it to page=2 or size=8. Whats the easiest way to have a link do that without reseting all the other vars?
I hope that makes sense, let me know if I need to further explain anything
You can parse the url with parse_str to get the values of the url. You can then build a http query by using http_build_query:
$query_arr = $_GET; //or parse_str($_SERVER['QUERY_STRING'], $query_arr)
$query_arr["page"] = 2;
$query_arr["size"] = 8;
$query = http_build_query($query_arr);
EDIT: Sorry I mixed up the two functions ... its parse_str() of course.
http_build_query

some quick help with this small piece of code

How come when I echo $p, the variable which Im trying to fetch using this loop doesnt get displayed in the path.
$name_image2="picture.jpg";
for ($i=2; $i<=$nr_of_pics; $i++){
$img='name_image'.$i;
echo $$img; gives me this: 'picture.jpg' which is correct.
but when echoing $p like this:
$p="/SV/main/temp_images/$$img"; echo $p;
I get this: SV/main/temp_images/name_image2 --> the variable 'name_image2' doesnt get called here, why?
I want it to say: SV/main/temp_images/picture.jpg
Thanks
$p = "/SV/main/temp_images/" . $$img;
Ought to fix it.
Also, I would recommend learning how to use arrays. They are a much better way to have a set of data instead of variable variables.
Try $p="/SV/main/temp_images/{${$img}}";
When PHP is parsing the string and comes to a $, it looks at the next character to see if it makes a valid variable name. If not, it moves on. In this case, that means that the second $ is correctly interpreted, but the first one has already been passed by. The answer is to enclose the inner expression in brackets, so that it will be parsed before the outer one is.

Categories