Page 1 of 2 12 LastLast
Results 1 to 10 of 50

Thread: Day 2 - Variables - Become PHP Expert in 30 days

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2003
    Posts
    3,040

    Default Day 2 - Variables - Become PHP Expert in 30 days

    Variables

    Variables are used to store data (String, Numbers, etc..) in a program.



    In PHP, variables start with symbol $. Examples for variable name

    PHP Code:
    $name
    $companyName 
    It is always better to use descriptive name for variables, so you know what is stored in a variable. For example, to store mark, we use variable $mark

    PHP Code:
    $mark 100// Good
    $m1 100// Bad, it will work but $m1, how we know what is stored in it ? It can be anything. 
    In above example, we stored numeric value in variable. When storing numeric value, we don't use quotes.

    Lets have some string variable

    PHP Code:
    $name 'Batman';
    $companyName 'FlashWebHost.com'
    Now lets have some real PHP examples

    day_2_variables_1.php

    PHP Code:
    <?php

    $companyName 
    'FlashWebHost.com';

    echo  
    $companyName;
    Now lets make it little more friendly with PHP string concatenation, that is joining two strings.

    day_2_variables_2.php

    PHP Code:
    <?php

    $companyName 
    'FlashWebHost.com';

    echo  
    'Welcome to ' $companyName;
    day_2_variables_3.php

    Another example with string and numeric variables.

    PHP Code:
    <?php

    $name 
    'Raju';
    $rank 1;
    $mark 1000;
    $school 'LP School';

    echo  
    $name ' from ' $school ' got rank ' $rank '. He have ' $mark ' marks.';

    Back to School, some simple maths

    Here are some basic numeric operations using PHP

    day_2_variables_4.php

    PHP Code:
    <?php

    $numCats 
    10;
    $numDogs 20;

    echo 
    'Total number of animals ' . ($numCats $numDogs);
    day_2_variables_5.php

    In this example we use multiplication operator in PHP to multiply 2 numbers.

    PHP Code:
    <?php

    $costOfApple 
    12.00;
    $numApple 8;

    echo 
    'Cost of ' $numApple ' apples = Rs: ' . ($numApple $costOfApple) . '/-';
    day_2_variables_6.php

    Lets do some billing.

    PHP Code:
    <?php

    $webhosting_price 
    399;
    $domain_price 500;
    $discount 0.1// 10% discount.

    echo 'Total cost = Rs: ' . ($webhosting_price $domain_price) . '/-';
    echo 
    '<br>';echo 'Price After discount = Rs: ' . ( ($webhosting_price $domain_price) -  (($webhosting_price $domain_price) *  $discount)) . '/-';
    That is lot of brackets, and repeated maths operation ($webhosting_price + $domain_price repeated 3 times), that is not efficient, lets simplify the code.

    day_2_variables_7.php

    Following code do exactly same as day_2_variables_6.php, but we added extra variable $total_cost, that store the value of $webhosting_price + $domain_price.

    PHP Code:
    <?php

    $webhosting_price 
    399;
    $domain_price 500;
    $discount 0.1// 10% discount.
    $total_cost $webhosting_price $domain_price;

    echo 
    'Total cost = Rs: ' $total_cost '/-';echo '<br>';
    echo 
    'Price After discount = Rs: ' . ( $total_cost -  ($total_cost *  $discount)) . '/-';



    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  2. #2
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Post example links, experiment (best way to learn) and discuss. No cut and paste please. Type the examples yourself, make mistake, try to fix it yourself, , think, refer back, post you code, so others can fix it for you. Lean from your mistakes (or others).
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  3. #3
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    http://php.flashwebhost.com/sherly/add.php

    Code:
    <?php
    
    $M = 20;
    $N = 50;
    echo $M + $N;

  4. #4
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by sherlyk View Post
    http://php.flashwebhost.com/sherly/add.php

    Code:
    <?php
    
    $M = 20;
    $N = 50;
    echo $M + $N;
    Quote Originally Posted by admin View Post
    It is always better to use descriptive name for variables, so you what what is stored in a variable. For example, to store mark, we use variable $mark
    It is ok, but better use descriptive names for variables. One letter variable names can be confusing in most cases.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  5. #5
    Join Date
    Sep 2003
    Location
    india
    Posts
    11,527

    Default

    I got an error while adding break in http://php.flashwebhost.com/annie/company.php

    Code:
    Parse error:  syntax error, unexpected '>' in /home/fwhphp/public_html/annie/company.php on line 3 
    Code:
     <?php
    $companyname = 'Relience';
    echo $companyname <br> ; 
    $companyname = 'Whirpool'; 
    echo $companyname ;

  6. #6
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    I got an error http://php.flashwebhost.com/sherly/name.php

    Code:
    Parse error:  syntax error, unexpected '' ;' (T_ENCAPSED_AND_WHITESPACE), expecting ',' or ';' in /home/fwhphp/public_html/sherly/name.php on line 7
    Code:
    <?php
    $my_string = 'Hi Meera. My anme is Neena';
    $my_number = 8;
    $my_letter = N;
    echo $my_string;
    echo $my_number;
    echo $my_letter' ;
    Last edited by sherlyk; 05-09-2014 at 07:08 AM.

  7. #7
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by annie View Post
    I got an error while adding break in http://php.flashwebhost.com/annie/company.php

    Code:
    Parse error:  syntax error, unexpected '>' in /home/fwhphp/public_html/annie/company.php on line 3 
    Code:
     <?php
    $companyname = 'Relience';
    echo $companyname <br> ; 
    $companyname = 'Whirpool'; 
    echo $companyname ;
    Problem is on line number 3.

    Code:
    echo $companyname <br> ;
    To fix, replace that line with

    Code:
    echo $companyname .  '<br>' ;
    $companyname is a variable. We use DOT to join it with string <br>. String should start with a quote, end with a quote.

    As for variable name, proper naming convention is $companyName, because it is two words, and easy to read.


    Fixed code

    Code:
    <?php
    
    $companyName = 'Relience';
    
    echo $companyname .  '<br>'; 
    
    // here we store another text to same variable $companyName
    $companyName = 'Whirpool'; 
    
    echo $companyname ;
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  8. #8
    Join Date
    Sep 2003
    Location
    india
    Posts
    11,527

    Default

    http://php.flashwebhost.com/annie/day_2_variables_3.php

    Code:
     
    <?php
    $name = 'George';
    $Rank = 1;
    $Mark = 800;
    $School = 'Raja Giri Public School';
    echo $name . ' from ' . $School . ' got Rank ' . $Rank . ' He have ' . $Mark . 'Marks';

  9. #9
    Join Date
    May 2014
    Posts
    21

    Default

    Quote Originally Posted by annie View Post
    I got an error while adding break in http://php.flashwebhost.com/annie/company.php

    Code:
    Parse error:  syntax error, unexpected '>' in /home/fwhphp/public_html/annie/company.php on line 3 
    Code:
     <?php
    $companyname = 'Relience';
    echo $companyname <br> ; 
    $companyname = 'Whirpool'; 
    echo $companyname ;
    Use a concat operator after $companyname and also put the <br> in quotes ($companyname.'<br>'; )

    Code:
    <?php$companyname = 'Relience';
    echo $companyname.'<br>'; 
    $companyname = 'Whirpool'; 
    echo $companyname ;

  10. #10
    Join Date
    Feb 2005
    Location
    India
    Posts
    11,004

    Default Same variable name for multiple values

    Quote Originally Posted by annie View Post
    I got an error while adding break in http://php.flashwebhost.com/annie/company.php

    Code:
    Parse error:  syntax error, unexpected '>' in /home/fwhphp/public_html/annie/company.php on line 3 
    Code:
     <?php
    $companyname = 'Relience';
    echo $companyname <br> ; 
    $companyname = 'Whirpool'; 
    echo $companyname ;
    Doubt 1
    Is it possible to use same variable name for multiple values ?

    Doubt 2
    Is variable name is case sensitive ?

    I mean both the following variables will give different values in the same script ?

    $variableName
    $variablename
    VIDEO WORLD : LATEST HOLLYWOOD || BOLLYWOOD || SOUTH INDIAN VIDEOS || TRAILERS

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •