HomeFAQSearchRegisterLog in

Share | 
 

 Understanding PHP.

View previous topic View next topic Go down 
AuthorMessage
Spellarella
Lifer
Lifer
avatar

Posts : 3905
Join date : 2009-08-16
Location : Peeking out of a drain.

PostSubject: Understanding PHP.   Sat Feb 12, 2011 7:16 am

I'm putting this up,to see if people understand it or whether it's got to be dumned down. That's not saying folks are thick, its just like anything new, sometimes you get the meaning other times you're screaming for a simple instuction booklet.

Pleae don't try the bits inclued, php can throw a tantrum and you can end up with a huge mess you struggle to get out of.

Quote :
Variables can store data of different types, and different types of data can do different things. For example, you can add variables whose values are numbers (1 + 2), but adding variables whose values are characters (a + b) doesn't make much sense.

You can store the following simple types of data in PHP variables:

•Integer: A whole number (no fractions), such as –43, 0, 1, 27, or 5438. The range of integers that is allowed varies, depending on your operating system, but in general, you can usually use any number from –2 billion up to +2 billion.
•Floating point number: A number (usually not a whole number) that includes decimal places, such as 5.24 or 123.456789. This is often called a real number or a float.
•Character string: A series of single characters, such as hello. There is no practical limit on the length of a string.
•Boolean: A TRUE or FALSE value.
Boolean data types represent two possible states — TRUE or FALSE. Boolean values are used mainly to compare conditions for use in conditional statements. For example, PHP evaluates an expression, such as $a > $b, and the outcome is either TRUE or FALSE.
PHP considers the following values FALSE :

Code:
•The string FALSE (can be upper- or lowercase)
•The integer 0
•The float 0.0
•An empty string
•The one-character string 0
•The constant NULL
Any other values in a Boolean variable are considered TRUE. If you echo a Boolean variable, the value FALSE displays as a blank string; the value TRUE echoes as a 1. Functions often return a Boolean variable that you can test to see whether the function succeeded or failed.

Assigning data types
Most other languages require that you initialize the variable before using it, specifying what type of data it can hold, but PHP is more informal. You don't need to tell PHP which data type is in a variable. PHP evaluates the data when you assign it to the variable and then stores it as the appropriate type. Generally, this is helpful. PHP guesses the data type pretty accurately.

PHP also converts data when it needs to be converted. For example, if you have the following statements, PHP converts the data types with no problem:

Code:
$firstNumber = 1; # PHP stores it as an integer
$secondNumber = 1.1; # PHP stores it as a float
$sum = $firstNumber + $secondNumber;
Technically, the third statement is not possible because the data to be added are different types. However, PHP converts the integer to a float so that the addition proceeds smoothly. This happens automatically and invisibly and is very helpful.

Type casting
On a rare occasion, PHP guesses badly when it stores the data. You might need to do something with a variable, and PHP won't let you because the data is the wrong type. In such a case, you can specify how you want PHP to store the data, rather than let PHP decide for itself. This is called type casting. To specify a particular type, use a statement like one of the following:

Code:
$newint = (int) $var1;
$newfloat = (float) $var1;
$newstring = (string) $var1;
The value in the variable on the right side of the equal sign is stored in the variable on the left side as the specified type. So the value in $var1 is stored in $newint as an integer, as specified by (int).

Be careful when doing type casts. Sometimes you can get unexpected results. For example, when you cast a float into an integer, it loses its decimal places. To do this, PHP rounds the float toward 0. For example, if $number = 1.8 and you cast it into an integer — $newnumber = (int) $number — $newnumber will equal 1.
You can find out the data type of a variable by using a statement like the following:

Code:
var_dump($myvariable);

For example, the following statement checks the data type of $checkvar:

var_dump($checkvar);
The output from this statement is int(27), which tells you that $checkvar contains the integer 27.


Now does anybody understand that or what area are you wanting to come to grips with?




Back to top Go down
.tUrniP
Lifer
Lifer
avatar

Posts : 910
Join date : 2009-08-13

PostSubject: Re: Understanding PHP.   Sat Feb 12, 2011 8:05 am

I must thank you for taking the time... I for one am ready to move on.

By the way, did you write this?

Back to top Go down
Spellarella
Lifer
Lifer
avatar

Posts : 3905
Join date : 2009-08-16
Location : Peeking out of a drain.

PostSubject: Re: Understanding PHP.   Sat Feb 12, 2011 8:41 am

.tUrniP wrote:
I must thank you for taking the time... I for one am ready to move on.

By the way, did you write this?

I can't take the credit for this, somebody else wrote it up as I was walking em through some php and it made sense than me staring blankly at a lne of code wondering what on earth was wrong. . I just kept a copy to hand as its was a handy reminder. I'm more of a do that, see, done, havent got the patience to write it all down. Some I do some I don't. Hence why its in quotes.
Back to top Go down
.tUrniP
Lifer
Lifer
avatar

Posts : 910
Join date : 2009-08-13

PostSubject: Re: Understanding PHP.   Tue Oct 18, 2011 4:14 pm

I doubt you have any interest in this now ( or even then to be honest ) but maybe you could do something on design patterns...? grin
Back to top Go down
Spellarella
Lifer
Lifer
avatar

Posts : 3905
Join date : 2009-08-16
Location : Peeking out of a drain.

PostSubject: Re: Understanding PHP.   Wed Oct 19, 2011 4:45 pm

Not lost interest at all. Busy with new hobby and health issues.

design patterns? What specifically do you mean?
Back to top Go down
.tUrniP
Lifer
Lifer
avatar

Posts : 910
Join date : 2009-08-13

PostSubject: Re: Understanding PHP.   Wed Oct 19, 2011 5:08 pm

Hmm ... hobby + health issues = serial murder - the issue being the victims death? look out

By design patterns I mean, singletons, decorators, adaptors, factories, et al...

For example - it's probably a good idea for a database class to use the singleton pattern so that there is only ever one database connection making things simpler and using less resources.

Back to top Go down
Spellarella
Lifer
Lifer
avatar

Posts : 3905
Join date : 2009-08-16
Location : Peeking out of a drain.

PostSubject: Re: Understanding PHP.   Thu Oct 20, 2011 4:52 pm

In an ideal world yes, and this is not my serial killer hobby either. freddy

Databases are fickle and have the tendancy to do entirely the opposite of what you intended. Espeically if you think only one thing is being implemented.

Take a look at the issue regarding updating PHPB forurms on the databases. For every mutation or add on applied, the database becomes unstable and more often crashes.

All is down to the database, what is is being used for and by which component parts and in which language or field. You've got to really understand the beast you're unleashing to work out what is needed.

Make a list of what you want to do, the end goal, and then work back to find what bits n bobs will be man enough to cope without the inevitable sulk and meltdown.

Tables, queries, access, security,in/output, files,form requirements,links state, directories, relationships, website requirements, modify instruction, . server loads, local hostings, redirections, orders, inserts, fetch arrays, back-ups, incremetnal status, retirevals the list goes on. But you still need to know what you require the database to do.

There probably is a dummies guide to databases, a good starting point is to read then deisgn on paper your eventual website before embarking down the database rollercoaster. Read, read and then wade in. most of all good luck. I think my killer of cerals will be easier. freddy

Or find a cheap host with one built in already and build and practise on that. Form mistake grow many good sites and less geek headaches.




Back to top Go down
buttercup

avatar

Posts : 219
Join date : 2012-02-05

PostSubject: Re: Understanding PHP.   Wed Mar 14, 2012 12:39 pm

Right.. So this is the sort of functionality I meant ( it's probably a terrible way to go about it but it's what I use ).

Code:

<?php

include(LIB_PATH . 'Database/DatabaseConnection.php');

class ConnectionManager {
 
    private static $connections = array();


    private function __construct()    {}

    private function __clone()    {}

    public static function connect( $dbType, $dbHost, $dbName, $dbUser, $dbPass )  {

        $_dsn = "$dbType:host=$dbHost;dbname=$dbName";

        if (!isset( self::$connections[$_dsn] )) {
            self::$connections[$_dsn] =  new DatabaseConnection( $_dsn, $dbUser, $dbPass );
        }

        return ( self::$connections[$_dsn] instanceof DatabaseConnection ) ? self::$connections[$_dsn] : false;
    }



}
?>

Does it make sense?
Back to top Go down
Sponsored content




PostSubject: Re: Understanding PHP.   

Back to top Go down
 
Understanding PHP.
View previous topic View next topic Back to top 
Page 1 of 1
 Similar topics
-
» Understanding Halloween
» Understanding Pharmaceuticals
» Understanding the Ukrainians in WW2 Part 3. Of German plans and German collaborators.

Permissions in this forum:You cannot reply to topics in this forum
 :: Bits For PC's :: PC Problems and solutions.-
Jump to: