wiki:How Do I Php

How Do I php?

Assuming the web server is properly configured to run php, then all you have to do is make a new file "filename.php" and save it to a location that can be accessed at a URL via a web browser. Log into your csc shell account; in your home folder will be a folder called WWW. You can place your files there to try out, and can see it in your browser at users.csc.tntech.edu/~yourusername/filename.php

The actual file could be blank, or have HTML in it with absolutely no php code and it will still serve the file. When you do use php code though, it must be wrapped in the tags:

<?php ?>

Or:

<?php

?>

If the closing tag appears at the end of the file, it may be omitted. Most of the CodeIgniter? files do not end in a closing tag. They claim it makes for better performance, since any whitespace after the closing tag would still be parsed by the webserver until it finds the 'true' end-of-file.

Here are some valid examples:

No php

<html>
<head>

</head>
<body>

	<h1>Hello World</h1>

</body>
</html>

inline php

<html>
<head>
	<title><?php echo "This is a new page title";?></title>
</head>
<body>

	<h1><?php echo "Hello World";?></h1>

</body>
</html>

More inline php

<?php

$title = "This is another title";
$str = "hello world";

?>

<html>
<head>
	<title><?php echo $title;?></title>
</head>
<body>
	<h1><?php echo $str;?></h1>

</body>
</html>

inline code like this does not require ending semicolons if it is only 1 line, but is good practice.

More inline php w/ functions

When you declare functions, you don't need to specify a return type (for the most part, php types variables implicitly), if it doesn't have a return stmt, it is void by default. Defining a value in the function parameters will make the parameter optional.

<?php


$str = "hello world";

/*
comments are like 
C and Java

*/
function print_title()
{
	//comments are like C and Java
	echo $title;
}

function get_str($str = "hello world")
{

	return $str;

}


?>


<html>
<head>
<title>
<?php

//line breaks not required
 get_title();

?></title>
</head>
<body>

<h1><?php echo get_str();?></h1>

</body>
</html>

Straight-up php

<?php

$title = 'another title'; //php can use single or double quotes, but there are different rules

echo("


<html>
<head>
<title>$title</title>
</head>
<body>

<h1>Hello World</h1>

</body>
</html>"); // parenthesis not required

?>
  • String concat operators are full stop '.' or comma ','
  • When using double quotes, you can place $vars directly inside them.
  • You cannot do this with single quotes, must use 'some text' . $var . 'more text'

More on strings later.

PHP ternary operators

If you have a bunch of variables you need to check, you can use ternary operators to reduce the code and make things look cleaner.

In general, the syntax goes like : $variable = condition ? if true : if false

So if you have:

if(isset($derpderp))
{
  $v1 = $derpderp;
}
else
{
  $v1 = '$derpderp is not set.';
}

if(isset($i_herp) && isset($when_i_derp))
{
  $v2 = $i_herp.$when_i_derp;
}
else
{
  $v2 = 'missing $i_herp or $when_i_derp or both.';
}

echo $v1 .'<br />'. $v2;

This is the equivalent:

$v1 = (isset($derpderp))? ($derpderp) : ('$derpderp is not set.');
$v2 =
  ((isset($i_herp)) && (isset($when_i_derp)))?($i_herp.$when_i_derp):('missing $i_herp or $when_i_derp or both.');


echo $v1 .'<br />'. $v2;

Note - the single quotes matter in this example; double quotes would treat the enclosed $ as an inline variable, and would defeat the purpose of checking isset().

The parenthesis after the ? are optional, but help make the code easier to read.

Last modified 7 years ago Last modified on Apr 4, 2011, 12:38:18 AM