Tips Station Asp.net Article Seo Articles
Tutorials Code Samples
›  Home
›  Mission
›  About us
›  Contact Us
›  Feedback
›  Terms & Condition
Asp Articles
IT Solutions
 
› ASP.NET

› Programming Tips

› Ajax

› Asp

› ADO.NET

› Databases

› SEO

› CSS And Designing

› Php

 
Most Viewed Articles
 
› Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

› Change theme dynamically without page refresh in ASP.NET

› Simple ASP Image Resize Function

› Install AJAX On Machines Running Visual Studio 2005

› ASP.Net Interview Questions And Their Answers

› Creating Pretty Popups Using AJAX

› Encode Url using ASP

› SQL Server Performance Counters

› .NET Data Access Performance Comparison

› UpdatePanel control in Asp.net Ajax

more...
 
 
Home> Php
 
Intro To Object: Option Variables
 

Now that we have our first simple class (which we build in Intro To Object: Building Your First Class) we're going to modify it a bit.

We're going to use the same class and function as in the last tutorial, but these will be slightly modified. We're going to make it where we can do different code based on the variables passed to the function. Again I'll refer to my db class as an example.

you have a function to do select * from table
now if you use mysql just a slight bit, you should know this is a very generic query, what if we want:
select * from table where this = '$this' limit 5
or even
select distinct(row) from table where this = '$this' limit 5

Now you could make different functions to do each, but that's a waste of time, and code. so what we do is make our function where we can say, if this variable is set, do this code. So, enough with the explanation, let look at the code

For the files again we have func_math.php:

<?
class math {

function add($num1, $num2, $num3 = ''){
if($num3 != ''){
$sum = ($num1 + $num2) * $num3;
$result = "($num1 + $num2) x $num3 = $sum";
} else {
$sum = $num1 + $num2;
$result = "$num1 + $num2 = $sum";
}
return $result;
}
}
?>

and, what I call math.php (you can name this whatever you like)

<?

require_once 'func_math.php';

$obj_math = new math();

$sum = $obj_math->add("2", "4", "4");
echo $sum;

?>

Since this is almost the same code as part two, im not going to break everything apart as much, just the key areas.

First lets look at func_math.php. Notice in the function math() we have a 3rd variable, which looks like this

$num3 = ''

This is setting up an option variable (theres really no official name for this that I know of, so I just call it an option variable). What this lets you do is, basicly break your function into two parts depending on if this variable is passed or not. Which is done with this code:

if($num3 != ''){
$sum = ($num1 + $num2) * $num3;
$result = "($num1 + $num2) x $num3 = $sum";
} else {
$sum = $num1 + $num2;
$result = "$num1 + $num2 = $sum";
}

Pretty much we say if $num3 is not = (!=) to "" (noting) do this, else do that. You could also do this the oposite way, if $num3 is = (==) to "" (nothing) do this, else do that. Its just a personal preference.

Now to call this function, all we do is add a thrid variable when we call it (in math.php), like this:

$sum = $obj_math->add("2", "4", "4");

Now an example of what this would return.

we do this:

$sum = $obj_math->add("2", "4", "4");
we get this:
(2 + 4) x 4 = 24

we do:

$sum = $obj_math->add("2", "4");

and get

2 + 4 = 6

Thats all for now. stay tuned, next time Im going to show how to build a image upload/resize class.

 
 
Vrp Technologies
 
Serversea Hosting
 
 
Latest Articles
 
› Sending SMS With PHP

› MySQL Join Tutorial

› Make An RSS Feed Using PHP

› Intro To Object: Option Variables

› Design An Online Chat Room With PHP And MySQL

› Create Tell A Friend Script With HTML & PHP

› Benchmark And Optimize PHP Script Speed

› What Kind of DBA Are You?

› SQL Server Performance Counters

› SQL Server Performance Tips

more...
 
Random Articles
 
› How to crop an Image in Asp.Net

› Search Engine Friendly Web Design - 3 Ways To Use CSS

› Read and display a text file in Asp.net

› What Are Active Server Pages (ASP)?

› Create Tell A Friend Script With HTML & PHP

› Cache Data within your Application - Asp.Net

› Benchmark And Optimize PHP Script Speed

› Encrypt Applications Settings in Asp.Net

› Make An RSS Feed Using PHP

› Choosing a Web Site Hosting Provider for an ASP.NET Site

more...
 
Home Mission About us Contact us Feedback Terms Conditions
2008 © Copyright TipsStation. All rights reserved.