Anyone know about PHP?
#1

I'm learning it from a book, but the book is 10 years old so I guess this is one of the occassional problems I'll find with it. Here's the code:

Код:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml1" xml:lang="en" lang="en">
<head>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
	<title>Predefined Variables</title>
</head>
<body>
<?php

# Script 1.6 - predefined.php
# Created by Sam
# Created April 6, 2013

// Echo the script name.
echo "You are running the file <b> $PHP_SELF </b>.<br /><br />\n";

// Echo the user's information.
echo 'Your are viewing this page using:<br /><b>', $HTTP_USER_AGENT, '</b><br />from the IP address ', $_REMOTE_ADDR;
?>
</body>
</html>
Basically the location comes up as . and the user PC details and IP don't come up...
Reply
#2

Use the $_SERVER superglobal (See: http://php.net/manual/en/reserved.variables.server.php)
Reply
#3

Код:
<?php
$P_IP = $_SERVER['REMOTE_ADDR'];
$P_UA = $_SERVER['HTTP_USER_AGENT'];
$Script = $_SERVER['PHP_SELF'];

echo $P_IP, $P_UA, $Script;
?>
$P_IP will be the users IP Address.
$P_UA will be the users browser details
$Script would be the information about the PHP file.
Reply
#4

Uh for this lesson I'm trying to use the default PHP variables not create my own yet.

So I changed it, well line 16 and now my site has an error. I only changed the file variable for now to make sure that is the one which was failing: http://phpwork.net78.net/predefined.php

Код:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml1" xml:lang="en" lang="en">
<head>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
	<title>Predefined Variables</title>
</head>
<body>
<?php

# Script 1.6 - predefined.php
# Created by Sam
# Created April 6, 2013

// Echo the script name.
echo "You are running the file <b> $_SERVER['PHP_SELF'] </b>.<br /><br />\n";

// Echo the user's information.
echo 'Your are viewing this page using:<br /><b>', $HTTP_USER_AGENT, '</b><br />from the IP address ', $_REMOTE_ADDR;
?>
</body>
</html>
Reply
#5

Replace line 16:

echo "You are running the file <b> $_SERVER['PHP_SELF'] </b>.<br /><br />\n";

TO:

echo "You are running the file <b>"; echo $_SERVER['PHP_SELF']; echo "</b>.<br /><br />\n";
Reply
#6

How does it work with single quotations?- Oh it works fine how it is.

So why doesn't it work fine inside the double quotations like it says in my book, is this a change in the new version of PHP?
Reply
#7

I'd also suggest using something more recent as a learning resource; PHP has come a long way in 10 years. Try PHP the right way: http://www.phptherightway.com/
Reply
#8

Alright thankyou. It's a shame because the book I'm reading is very clear and something I can actually understand.
Reply
#9

Quote:
Originally Posted by Coltmaster
Посмотреть сообщение
How does it work with single quotations?- Oh it works fine how it is.

So why doesn't it work fine inside the double quotations like it says in my book, is this a change in the new version of PHP?
There is no difference between single and double quotes in php. It's just good practice to use single quotes for arrays/indexes and double qoutes for declarations of strings (although it's more of a personal preference - I use single quotes as much as I can).

For example, it's common to do:
PHP код:
$_GLOBALS['some_index'
and:
PHP код:
$string "This is a string"
EDIT: There isn't a difference in performance either (not in the new PHP versions anyways).
http://nikic.github.io/2012/01/09/Di...ance-Myth.html
Reply
#10

Quote:
Originally Posted by Sinner
Посмотреть сообщение
There is no difference between single and double quotes in php. It's just good practice to use single quotes for arrays/indexes and double qoutes for declarations of strings (although it's more of a personal preference - I use single quotes as much as I can).

For example, it's common to do:
PHP код:
$_GLOBALS['some_index'
and:
PHP код:
$string "This is a string"
EDIT: There isn't a difference in performance either (not in the new PHP versions anyways).
http://nikic.github.io/2012/01/09/Di...ance-Myth.html
https://www.youtube.com/watch?v=kj0JwIdJS0o

This is a little out-dated but it shows a difference between the two.

But you can code however you want.. that's just a tip.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)