Warning: include(../server_wide_scripts/sub_t_logs.php) [function.include]: failed to open stream: No such file or directory in /home/briant97/public_html/php-sample.com/wp-content/themes/panorama/header.php on line 6

Warning: include(../server_wide_scripts/sub_t_logs.php) [function.include]: failed to open stream: No such file or directory in /home/briant97/public_html/php-sample.com/wp-content/themes/panorama/header.php on line 6

Warning: include() [function.include]: Failed opening '../server_wide_scripts/sub_t_logs.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/briant97/public_html/php-sample.com/wp-content/themes/panorama/header.php on line 6
PHP Sample » Real Life Solutions – Tutorials – Examples

Having Fun with the Date Function

By Ark, June 28, 2010 1:17 am

As an interactive form of conveying information to your visitors, this add-on through PHP scripting will allow them to easily know the date. The date function can show recent blog posts and times when you’ve posted news or updates. This is convenient when you want to form an archive and a sense of timeliness when you write your blog. Here is how the date() works.

Let’s say you want to show today’s date. In PHP coding, it would look similar to this:

<?php
echo date("m / d / Y");
?>

When you view it in your browser, it would look something like this:

Output of Date

06 / 28 / 2010

As you can see, you have shown the date conveniently on your website. This becomes very efficient when it comes to news websites that usually have the date found in the header. The date() function is fun to play with because you can diversify its output using different methods and combinations of dates. What if you desire to twist around your date?

In fact, in my experience in web design, I have encountered a very common problem regarding future dates. Here’s how it goes.

10 Days After Problem

How can you script a code that informs the user of a product promotion that expires in 10 days? I want to show the exact date when the promo expires without modifying each product? Like “this product ends in ten days or –insert date here–.”

You see, there is a method within the date function that works exactly that way. This is known as the time stamp option. In fact, it works conveniently for inputting exact dates, like those ten days in advance. The syntax would look something like this:

<?php
$ten_days = mktime(0,0,0,date("m"),date("d")+10,date("Y"));
echo "The product promotion ends on ".date("m/d/Y", $ten_days);
?>

The input would go something like this (provided the date is June 28, 2010):

Output of 10 Days Date

The product promotion ends on 07/08/2010.

As you can see, you can easily solve the problem of the ten days repetition. A better understanding of the syntax of the timestamp can be seen here:

mktime(hour,minute,second,month,day,year,is_dst)

Just adjust the date as you see fit and in the portion of “day”, just add +10 or another increment to show the date in ten days’ time or whatever amount you wish. This method is effective for promos or other things.

As you can see, the date() function is dynamic when it comes to showing the date, you can effectively set it to the current date for your news updates, or perhaps add a twist to the promo end of it all. You may conveniently check the PHP manual on different date formats you can try out. The date() function indeed becomes an interesting PHP function for anyone’s desire–perfect for situations such as news updates of any kind or your simple notices that you wish to make evident.

Versatility of the If Conditional in PHP

By Ark, June 19, 2010 9:42 am

Have you ever wanted to know if some data is true or false? This fundamental concept represents the if conditional, which can be expanded to the popularly used if…else conditional statement. Even more expansive is the if…else…if statements which can support different conditions for your code. Things can be determined by the conditions that are needed to run your code. These conditions are shouldered by the if() command that checks against a required input. When you want to determine things such as user-inputted data, if conditionals are effective and simple ways of doing that.

The if conditional syntax looks like this:

if(condition)
	{
	//execute code here
	}

For if…else statements, the code syntax would flow more like this:

if(condition)
    {
    //execute code here
    }
else
    {
    //default code if condition is not met
    }

Thirdly, the if…else…if statements syntax would look something like this:

if(condition 1)
	{
	//execute condition 1 code here
	}
elseif(condition 2)
	{
	//execute condition 2 code here
	}
//as many elseif blocks as you need...
else
	{
    // default code if none of the conditions are met
    }
Conditions

Conditions are arguments that your code checks for. Usually, these are arithmetic arguments that compare if a value is equal to, less than, or greater than a certain value. These conditions are applied to if conditional statements to determine a code.

Let’s take my code for example. In my web page, I want to find out if a number visitors inputted matches the choice of design that they choose. I know there could be other ways, but I chose to use the if conditional to simplify it. Here’s how it looked:

if($designchoice == 1)
	{
    echo "You picked the first design";
    }

My variable was $designchoice and the parameter selection was 1. If I wish to expand it to add a second choice, my code would look like this:

if($designchoice == 1)
    {
    echo "You picked the first design";
    }
else
	{
    echo "You picked the second design";
    }

This is restrictive, so I want to make three design choices. This can be conveniently done with the if…else…if statement. Here’s how it would look like.

if($designchoice == 1)
	{
    echo "You picked the first design";
    }
elseif($designchoice == 2)
	{
    echo "You picked the second design";
    }
elseif($designchoice == 3)
    {
    echo "You picked the third design";
    }
elseif($designchoice == 4)
	{
    echo "You picked the fourth design";
    }
Example output if $designchoice is 3

You picked the third design

Of course, that will help me arrange my design files for later use. The if conditional is effective because it helps me choose which code (or page) to run. Depending on the purpose for your code, the if conditional gives you an easy approach to filtering out settings that you wish to place. The simple syntax can be expanded to accommodate other functions you wish to use. The versatility of the if conditional in PHP, indeed, becomes a universal function where you can add more dynamism to your web page.

Top Three Reasons to Use PHP

By Ark, June 17, 2010 2:19 pm

Top Three Reasons to Use PHP

PHP has been widely perceived as one of the most accessible coding languages of the internet today. You may be wondering why people choose to use this language for their web applications, other online programs, and most of all, for their database storage systems. PHP has the advantage over most coding languages that make it an ideal language to program with. Here’s an outlook on the three top reasons why you should use PHP.

PHP works in sync with HTML and MySQL

PHP is a language that fits best with Hyper Text Markup Language (or HTML) and MySQL. HTML is the core function of all website applications, so it’s always an advantage for a coding language to fit in with the web standard. PHP works seamlessly with HTML because you can integrate PHP code into the HTML source code. Even more significant in this integration is the fact that PHP can generate HTML code. This is done using some popular functions in PHP such as the echo() function.

MySQL is a database management language that works best in line with PHP. The advantage of this database management system is that it fuels most websites that require these functions. Have you checked your favorite social media site lately? Most likely, it runs on PHP. But where are your posts and other information stored? The answer here is MySQL. You may notice how seamlessly the two codes work together.

PHP is Easy to Learn

Many people denote PHP as a difficult language to learn. In fact, it starts out very elementary because the coding syntax is similar to C or C++. In fact, it is C/C++ which PHP is rooted from. You will be familiar with the similar functions such as the for(), while(), and other functions present in C/C++. The learning process of PHP can be made efficient if you learn it parallel to its applications–such as its use in HTML and MySQL. The advantage of this will allow you to incorporate a wide array of knowledge of PHP alongside the other disciplines.

PHP is Dynamic

Lastly, PHP is a dynamic language that has forms of interaction with you and the site that you visit. PHP is a strong language that can welcome in many possibilities. Search around and you will witness the power of PHP. If you notice on many social media sites with extensions “.php” on the links, you will notice how fast each page loads or even how dynamic the content is. PHP works in line with other languages to hasten performance and give you top grade results. The dynamism of PHP paves the way for more innovations in the language that are yet to be explored.

PHP, indeed, is a very interesting language for your coding endeavors. It works in synchronicity with popular web languages, it is relatively easy to learn because of its familiar syntax, and it is a dynamic language that introduces new concepts in web-technology. If you’re considering a coding language in your web content, maybe it’s time you try out PHP.

PHP vs ASP – The Better Choice for Web Programming?

By Ark, June 13, 2010 1:20 pm

Using the programming language for your website can be quite convenient if you know what language to use. PHP has been widely acclaimed to be suited for most web applications. You may notice that popular social media sites such as Facebook and Twitter use PHP as the coding medium.

PHP has been compared to other coding languages that perform similar functions. One of these is ASP.Net. If you’re looking for the most appropriate language for your website, then here are some points to consider regarding the two coding languages.

ASP stands for Active Server Pages, and this explains how efficient the language is when it comes to transferring data from and to databases. PHP translates to PHP:Hypertext Preprocessor. The comparison begins by which language is more frequently used.

Operating Systems

If you consider the operating systems that utilize the languages, PHP is multi-platform to the point that it can run on Linux, Unix, Solaris, and Windows operating systems. On the other hand, ASP works only on Windows-based platforms and Linux provided that your Linux operating system has an ASP-Apache program. With this, you can see how PHP is more dynamic in terms of operating system use.

Programming Language

You can really decide to use PHP and ASP depending on your coding proficiency. People with background at C or C++ programming can adapt to the PHP coding because of similar syntax—this is due to the PHP using the C or C++ programming as its basis. ASP, on the other hand, uses the programming language similar to Visual Basic. This is noted by being appropriate for Windows-based servers which accommodate the same language. It all depends actually on which language you are proficient in.

PHP is more friendly in terms of object oriented programming, which makes it a useful language for many different purposes. ASP.Net also has support for OOP, but PHP is usually the preferred language to code in.

PHP establishes the popular connection with MySQL, a language used for database management. ASP supports that as well, but because of the proficiency and

Cost of Running

Deciding the cheaper alternative can be made using what factors are needed to run these languages. You may need to purchase the IIS server for ASP, and this may cost you. Regarding PHP, you can just simply run it on a Linux server, which comes at no cost at all. When it comes to database connectivity, PHP can freely run on most platforms; however, ASP needs some form of payments to run.

In conclusion, the two languages have their distinct uses. PHP has been shown to be more efficient as a coding language, but if you’re more comfortable with Windows-based server processing, then ASP is the preferred choice for you. It all comes down to your preference when coding using these two languages.

For more information regarding ASP vs PHP usage, you can click on the following websites–
- ASP vs PHP
- ASP vs PHP – Which one is Right For You
- Comparing PHP and ASP.Net

Insight of For Loops with PHP

By Ark, June 11, 2010 8:38 pm

Insight of For Loops

Whenever you want to code using PHP, you may need to repeat some processes. This is especially true if you want to update current events according to statistics, or simply for your visitor counter. The many things you can do with the for loop can range from the simplest to the most complex. Here’ an insight on how the for loop function works.
FOR LOOP Syntax

Remember, in order to understand how the code works, you have to break it down to its basic components. Observe this code I use for my marketing website on the price of my service:

<?php
echo "<table width='50%'><tr><td>Number</td><td>Price</td></tr>";
$base = 2;
for($rate=10;$rate < 100;$rate += 10)
	{
	echo "<tr><td>$rate</td><td>";
	echo ($rate*$base) - ($rate*.05);
	echo "</td></tr>";
	}
echo "</table>";
?>

What it produces is a table with the rates adjusted to the formula. But how does it explain for loops? Why is iteffective? You see, for loop will allow you to repeat the code using only one set up. This will save you the time and efforts taken to produce the code. We have the data of my base price at 2 in one variable $base. I also limited showing my prices for numbers lower than 100. Had I used simple code, I would have to copy/paste the line over and over again. And if I want to change something, that will be difficult. Here’s how for loops work:

for(condition;limitation;process)
	{
	argument or code to perform
	}

Here is how the code works:

Condition

The condition is a simple statement that initiates the for loop, you will find it in the first portion of the code. Usually, it sets where your code will start processing from.

Limitation

A loop can go endless if you don’t set any limitations. The second part of the for loop indicates a limit which is identified with an inequality. This will help keep your results within a certain range.

Process

This step will actually allow your code to engage in looping. This comes with the format of being written in an incrementation of a certain value.

When you put these three components together, you will have a code that starts with a value, processes and increases or decreases the value, and stops looping once it reaches the limitation.

The for loop works by weighing this data against a certain constant and if the connection fits, your code will process. You can observe how it works by my price-rate code. You can see the results:

Code output
Number Price
10 19.5
20 39
30 58.5
40 78
50 97.5
60 117
70 136.5
80 156
90 175.5

As you observe, the results are many yet the code only contains code for a single-cell of the table. For loops offer guidance for repeated codes. And the convenience is that you only have to edit one or two variables to change the whole data altogether. For loops can help me gather data with a pattern and conveniently tabulate them.

Other uses of the for loop can expand with MySQL integration to allow seamless processing on some websites such as forums or social media networks. The for loop is a very dynamic function that allows the convenience of repetition in concision.

Panorama Theme by Themocracy