Nov 5, 2009

Php Variable Concatenation - Something New I learned Today! :P

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

Php Variable Concatenation - Something New I learned Today! :P

szupie
I was coding one of my php page today, when I realized that I had to add multiple values to a variable (now that I think of it, a solution with arrays is possible too). But, the problem is that I have to add them in different parts of the code, so the new line that defines the variable will cover up the previous one. I played around with the code, and I finally got this solution:


$message = "test1";

$message .= "test2";

$message .= "test3";

echo $message;


By adding a dot in front of the = sign, I could concatenate the previous value with the new one and put them in the same variable. So... Yeah... This is just something I figured out, and I'm sure most people know about this. I just had to share... ;P

Oh, btw, a dot on the first = works too. It still "concatenates" the nothingness before it and the new value.

Comment/Reply (w/o sign-up)

marijnnn
that's in fact a basic programming trick.

just like this will give 9:

$number = 5;
$number +=4;

echo $number;

same thing for -=, /=, *=

Comment/Reply (w/o sign-up)

miCRoSCoPiC^eaRthLinG
There's another post that demonstrates the usage of this ".=" operator for concatenating strings... You might want to take a look at it:

http://www.astahost.com/index.php?showtopi...148&#entry18148

These are standard operators (Assignment OPs) found in almost any programming languages... Here's a brief list:

Addition: +=
Subtraction: -=
Multiplication: *=
Division: /=
Modulus: %=
Left shift assignment: <<=
Right shift assignment: >>=
Bitwise-AND: &=
Bitwise-exclusive-OR: ^=
Bitwise-inclusive-OR: |=

Comment/Reply (w/o sign-up)

szupie
Heh, just what I thought: A simple basic code. I have seen the .=/-=/+=/etc operators before, but I've never thought of using them. This proves that writing your own programs lets you learn fast!

Comment/Reply (w/o sign-up)

avalon
Cool! Never thought of using that them, maybe I should start using some of it.

Comment/Reply (w/o sign-up)

mastercomputers
Glad you've learnt concatenation, and the correct term too.

Other methods, without using the .= operator is we can join different variables into another variable.

e.g.

$string1 = 'Hello, ';
$string2 = 'World!';
$string3 = $string1 . $string2;
$string4 = $string1 . 'and welcome to my ' . $string2;

echo $string3 . ' ' . $string4;
//results: Hello, World! Hello, and welcome to my World!

The period/dot is our means of joining or linking our variables/strings/etc.
$var .= 'hey'; // means $var = $var . 'hey';

When you talk about concatenating NULL (nothingness) initialised variables, you should really make sure that is the case. All variables should be initialised as NULL but there's no gaurantee that this would be the case and if you concatenate a variable that you expected should have been NULL but wasn't, you might get undesirable or incorrect results.

It's safer to initialise the variable first then concatenate onto it.

Cheers,


MC

 

 

 


Comment/Reply (w/o sign-up)

szupie
Yeah, I've learned how to concatenate the way you just said, MC, that's why I knew the word. The first time I've heard of it, it was confusing (the word).

The +=/-=/%=/etc ones don't work for strings, right? They only work with numbers?

Comment/Reply (w/o sign-up)

vujsa
On a related topic,
.= has a derivitive.
. (dot) can be used in complex variable assignments.

For example if you wanted to use an inline function use the following:
$html = "PI = ".number_format(M_PI,"2",".","")." = ".M_PI."!";

Where the RED code is outside of the quotes so that the function will execute correctly. Otherwise the output would read:
PI = number_format(M_PI,"2",".","") = M_PI!
Instead of:
PI = 3.14 = 3.14159265359!

Other wise written:
CODE
$html = "PI = ";
$html .= number_format(M_PI,"2",".","");
$html .= " = ";
$html .= M_PI;
$html .= "!";


Just to recap.

The . (dot) means plus or and.

This allows strings to be mixed with functions etc...
Saves from repetitive coding and as a result time. Makes the code cleaner and easier to read when an entire string "sentence" is all together on the same line.

Speaking of eliminating repetitive coding, see the following post of mine:Rapid HTML code generation using simple PHP
Many other good PHP links in the same forum.

Good luck
vujsa

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : php, variable, concatenation, learned, today, p

  1. Permanent Variable
    (7)
  2. Send Php Variable To Javascript
    (5)
    Right i had a look across the internet as well as a search on here but you cannot search for
    anything less than 3 characters. But this is a really quick question. Would the following code
    allow me to send a php variable to a javascript? CODE $color = "green"; ?> BackColor= " ";
    ....
  3. Unexpected Error
    Undefined variable??? (2)
    Is this script correct? index.php : CODE     if (isset($_COOKIE ))           $_GET =
    $_COOKIE ;     else         setcookie("disp_name", Anonymous, date()+99); ?>              
    Display Name - DZN                                                                   
                    Dislpay Name: name="name" />                                                
         proccess.php : CODE      Proccessing Request...           Please wait...
        setcookie("disp_name", $_GET , date()+99); ?> main.php : CODE     function customErr....
  4. Php Any Variable In String.
    (1)
    OK well I am making a new php program and I am trying to add bbcode to it. Anyway I was going to
    replace each thing by it self, but that could cause errors. Anyway is there a way to make a variable
    be anything? Here is an example: CODE This is the bbcode: Hey In my php: $bbcode = array(" ");
    $html = array( " "); $topic_content = str_replace($bbcode, $html, $posted_bbcode); So I want $a
    to be any variable but the same variable as used before. Do you get what I am saying? It is a little
    confusing but basicly what is the best way to make bbcode? It may not even inv....
  5. Quickly Create Form Variables
    simple form, variable creation, referer check, safe guard variables (5)
    The reason I wanted to share this is I've seen so many people do this with their forms when
    using PHP. CODE $username = $_POST ; $password = sha1($_POST ); $another_var = $_POST ; ...
    and so on, just imagine if you had a large number of form inputs, do you really want to create each
    and every variable name? Why people do this, is probably due to most of the examples I've seen
    on the web, that does not show an easier and much quicker way of doing it. Though my way might be
    much easier and quicker, it does introduce security concerns which I've tried....
  6. Variable From Line Further Then Current Line?
    (13)
    Hello, Is it in some way possible to load a variable that further then the current line in the
    script? I don't know if you know what i mean so I'll try to point it out in the script
    below. CODE 1 2      echo $var1; 3      # $var1 needs to be loaded from foo.php 4     # Lets
    say i want $var1 to be echoed in $var1 5       - - - -       bla bla bla 55      - - - -
    56      switch($_GET ) { 57          case "foo": 58              include("foo.php");
    59              #- - - - from foo.php - - - - 60                   $var1 = "what ever you want";
    61           ....
  7. How To Reset The Server Variable Php_auth_user
    (9)
    Hi, i'm developing a web application which obviously requires a log in/log out script that i
    just implementing but i dont know why the log out script dont work fine. The problem is related
    with the server variable $_SERVER which remains set even when in the log out script i unset it with
    the unset() function. Does someone knows how can i reset or clear the server variable $_SERVER ???
    Best regards, ....

    1. Looking for php, variable, concatenation, learned, today, p

See Also,

*SIMILAR VIDEOS*
Searching Video's for php, variable, concatenation, learned, today, p
advertisement



Php Variable Concatenation - Something New I learned Today! :P

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com