14
2013
Getting Started with PHP: Part 3: Structure and variable existence
And here we are on part 3 of “Getting Started with PHP”!
Today we have…
- Using effective website structure to make your life easier
- Overview of PHP isset and empty commands
- Using PHP to do your heavy lifting (using variables)
Overview of PHP Isset && Empty Commands
Within PHP, it is important to determine if our variables ‘actually’ exist, and/or are specified in any number of formats.
For example:
$variable = NULL;
echo $variable;
Will output nothing (NULL). But what if we ONLY want to echo $variable IF $variable HAS a value?
Using PHP’s “isset” or “empty” commands, we are able to check the values of passed variables. Similarly, we may specify NOT empty and NOT isset by using the “!” operator (which is equal to NOT).
- if( !isset( $variable ) ) is equal to saying “if the variable $variable is not set”
- if( isset( $variable ) ) is equal to saying “if the variable $variable is set”
- if( !empty( $variable ) ) is equal to saying “if the variable $variable is not empty”
- if( empty( $variable ) ) is equal to saying “if the variable $variable is empty”
Differences between “empty” and “isset”
“empty” is most commonly used to check variables passed through the URL string, such as…
http://www.google.com?q=PHP+isset+variable
Empty checks for a value after the “?q=” variable in the above example using the “$_GET[‘q’]” method, as URL string variables are acquired via PHP’s $_GET command.
“isset” is used with POST values (aka information passed through a script from a form), and these values are not displayed in the URL string.
The easiest way to work with default and empty value handling is to assign a variable to the variable, where the first variable in-line is the default, as it will be overridden if a new value is located.
URL: https://www.phpdevtips.com/person.php?name=Bennett
$name = NULL;
if( !empty( $_GET['name'] ) )
{
$name = $_GET['name']; //Since a value exists, we re-assign the $name variable to the located value
}
And to check if a form has passed a value, we can do the following:
URL: https://www.phpdevtips.com/something
$name = NULL;
if( !isset( $_POST['name'] ) ) //checking to see if name was not passed from form
{
$name = "No name passed from form!"; //Since a value has been passed, we re-assign the $name variable to the posted value
}
Making Complex Sites Simple using Structure!
Best way to make website creation difficult- ignoring basic organization! Best way to tell the experience level of a developer? You guessed it! No organization.
index.php
styles.css
footer.inc.php
img1.jpg
img2.jpg
actions.js
dbc.php
The above example is a NO! Note that it’s nothing but a heap of images, pages, actions, and stylesheets.
index.php
includes
|
-----Images
|
------img1.jpg
|
------img2.jpg
-----styles
|
------styles.css
|
------forms.css
----JS
|
------actions.js
----constant
|
------dbc.php
|
------footer.inc.php
Above example looks complicated, but it will certainly save you some headaches in the future!
Tying it together using Constants
Quick refresher on constants:
define( "REL_BASE", $_SERVER['DOCUMENT_ROOT'] . "/yourfolder" );
//Above is equal to: /Applications/MAMP/htdocs/yourfolder (relative URL)
define( "BASE", "http://www.localhost:8888/yourfolder" );
//Above is equal to what it says (absolute URL)
Now provided you have included the constant in your page, or in an external file using include (‘file_with_constants.php’); you can easily include anything, from any folder, easily:
<link href="<?php echo BASE; ?>/includes/styles/styles.css.php" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<?php echo BASE; ?>/includes/js/jquery-1.6.1.min.js" language="javascript"></script>
PHP Math
PHP can do quite a bit of the heavy lifting for you when it comes to mathematical operations with a long list of functions. Included are…
- abs – Absolute Value
- ceil – Round fractions up (great for progress bars and other things where fractions don’t work well)
- floor – Round fractions down
- getrandmax – Calculate largest randomly generated value
- is_nan – Determines whether a given value is not a number
- max – Find highest value
- min – Find min value
- mt_rand – Excellent random number generation
- More here: http://php.net/manual/en/ref.math.php
You can also perform simple mathematics using symbols, and may include variables within equations:
- $twenty = 10 * 2;
- $ten = $twenty / 2;
- $twohundred = $twenty * $ten;
- $one_sixty = ( $twohundred / 15 ) * 12;
Related Posts
Leave a comment
Recent Snippets
- htaccess : Only allow access to specific wordpress upload types if logged in
- MySQL : Query WordPress Database for Invalid Media Filenames
- PHP : Get Actual IP Address with PHP
- JavaScript : Allow Tab in Textarea
- PHP : Clean sanitized database contents on output
- htaccess : Force www. prefix in URIs
- PHP : Force File Download with Correct Content Type
- Wordpress : Disable upgrade notification