Archive

Posts Tagged ‘PHP’

Using Optional Argument with User Defined Functions in PHP

March 11th, 2010 Mehmatrix No comments

Hi everybody. After a long time, I am here again with my new articles. I thought it can be good to start with a little clue for PHP coders.

Usage of PHP functions is so easy and practical. However, usually we need to write our own functions while we are making our applications.

1.1) User Defined Functions in PHP

Structure 1.0:

1
2
3
4
5
6
7
<?php
function FunctionName( arguments ){

//php codes

}
?>

The function ends when return() statement is executed. Click to Read Complete Article »

Bookmark and Share

Checking Database Connections and Getting Errors in PHP

November 25th, 2009 Mehmatrix 1 comment

We have talked about how to make database connections in PHP before. However, I realized that I have forgotten something which is so important. If you read that article or you are interested in PHP, you may know that we use odbc_connect() or mysql_connect() (or etc.) functions to connect some databases. These functions try to connect to specified databases. I wish every request would be accepted, because it is possible getting errors during the process. If the connection is not successful, the rest of codes will not work properly. So today, I will show you how to check database connections.

It is so simple checking database connections in PHP. We need to know that Click to Read Complete Article »

Bookmark and Share

Reading and Modifying PDF Files Using PHP

November 15th, 2009 Mehmatrix 1 comment

Today, we are going to talk about getting content of PDF documents. In addition, PDF is a document format of Adobe Acrobat developed by Adobe and abridgment of Portable Document Format. After giving this unnecessary information, let’s learn how to read PDF documents using PHP.

To open and read PDF documents via PHP, we need to use a package: TCPDF plug-in for reading PDF.

Reading and Modifying Content of PDF

You can download TCPDF package from here. This is a free package and you can use it easily. There are some examples from tecknick that show how to use this package:

Example 1.0: Creating a Simple PDF Document.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php

require_once('../config/lang/eng.php');
require_once('../tcpdf.php');

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 002');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);

// ---------------------------------------------------------

// set font
$pdf->SetFont('times', 'BI', 20);

// add a page
$pdf->AddPage();

// print a line using Cell()
$pdf->Cell(0, 10, 'Example 002', 1, 1, 'C');

// ---------------------------------------------------------

//Close and output PDF document
$pdf->Output('example_002.pdf', 'I');
?>

Click to Read Complete Article »

Bookmark and Share
Categories: PHP Tags: , , , ,

Database Connections in PHP

October 1st, 2009 Mehmatrix 1 comment


1.1) MYSQL Connection in PHP

We can use mysql_connect and mysql_select_db functions to create our connections. For closing the connections there is mysql_close function. These are basic functions of managing connections in PHP. There are also a lot of classes made by developers for free.

Sturcture 1.0:

1
2
3
4
5
<?php
$ConnectionName = mysql_connect('MyHost', 'MyLogin', 'MyPassword');
$DatabaseConnection = mysql_select_db('MyDatabaseName', $ConnectionName);
mysql_close($ConnectionName);
?>

You can determine the MyHost, MyLogin, MyPassword and MyDatabaseName variables as you need.

Example 1.0: We used hostname:port syntax Click to Read Complete Article »

Bookmark and Share

Generating Random Number in PHP

September 24th, 2009 Captain No comments

Generating random numbers is very important in programming. We have to use random numbers to show a random record (Image, Saying, Article etc.) on our pages. Also, we can use random numbers for everything that we can imagine. In this article, we will see easiness of generating random numbers with PHP.

Structure 1.0: This is basic statement(void function) and we don’t determine nothing like maximum or minimum limit.

1
< ?rand();?>

Example 1.0:

1
< ?echo(rand());?>

Probable Output of Example 1.0:

1
652696728

1.1) Generating a Random Number Between Two Numbers

Structure 1.1: If we want to generate Click to Read Complete Article »

Bookmark and Share

PHP Loops

September 23rd, 2009 Mehmatrix No comments

We are going to talk about all looping statements in PHP. We have four looping methods.

I. While loop
II. Do … While loop
III. For loop
IV. Foreach loop

Most of PHP statements are similar to C statements. So, a programmer who knows C, can learn PHP easily.

I. While Loop

While statement loops the operations while specified conditions are true. So, we can use this looping method when you don’t have or don’t know number of repetitions.

Structure 1.0 :

1
2
3
4
5
while( conditions ){
//…
//operations
//…
}

There is a simple code example that shows us how to use Click to Read Complete Article »

Bookmark and Share
Categories: PHP Tags: , ,
eXTReMe Tracker