6 Replies - 308 Views - Last Post: 13 March 2014 - 03:34 PM Rate Topic: -----

#1 asti80  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 28-February 14

Help can someone explain a PHP variable

Posted 12 March 2014 - 04:49 PM

Im reading a tutorial and I dont understand the text variable $text and $text.
How do I have to think.... How will this look?


$text= ('
<style type="text/css">
 h1{ margin: 10px; font-size: 24pt; }
 h2{ margin: 15px; font-size: 20pt; }
 
</style>
');
 
$text.=('<h2>'. $quotes[$i] .'</h2>');

Is This A Good Question/Topic? 0
  • +

Replies To: Help can someone explain a PHP variable

#2 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 1,816
  • Joined: 15-January 14

Re: Help can someone explain a PHP variable

Posted 12 March 2014 - 05:13 PM

First, there's no reason to have those parentheses there, this code will work the same and might be a little clearer:

$text= '
<style type="text/css">
 h1{ margin: 10px; font-size: 24pt; }
 h2{ margin: 15px; font-size: 20pt; }
 
</style>
';
 
$text.='<h2>'. $quotes[$i] .'</h2>';



The first assignment sets $text to be all of the text that you see there, plus a linebreak at the beginning and end, so first text will contain this:

<style type="text/css">
 h1{ margin: 10px; font-size: 24pt; }
 h2{ margin: 15px; font-size: 20pt; }
 
</style>



The second assignment uses .= which is the concatenation assignment operator, you can see it described here:

http://www.php.net/m...tors.string.php

In PHP, the . operator is to concatenate 2 strings, like the manual shows:

$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"


The .= is a shortcut, writing $a .= $b is the same as $a = $a . $b;

So, after that second assignment then $text will contain this:

<style type="text/css">
 h1{ margin: 10px; font-size: 24pt; }
 h2{ margin: 15px; font-size: 20pt; }
 
</style>
<h2>{$quotes[$i]}</h2>



The text "$quotes[$i]" will not be there, it will be the value of $quotes[$i] instead.
Was This Post Helpful? 0
  • +
  • -

#3 KruSuPhy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 30-June 10

Re: Help can someone explain a PHP variable

Posted 12 March 2014 - 05:17 PM

It'll look like the style you have <h2> set to, and it'll say whatever you have stores in $quotes[$i]. If I'm correct, the .= operator just adds '<h2>'. $quotes[$i] .'</h2>' to the end of what you originally set to $test.

EDIT: I have been ninja'd, and with a much better answer than my own. Disregard this post.

This post has been edited by KruSuPhy: 12 March 2014 - 05:17 PM

Was This Post Helpful? 0
  • +
  • -

#4 asti80  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 28-February 14

Re: Help can someone explain a PHP variable

Posted 12 March 2014 - 05:57 PM

BIG THANK U for the answers!! Really well explain I understood everything, another thing in the tutorial is this
$i= rand(0, sizeof($quotes)-1);
what does this do and the -1 do?
Im looking at this tutorial http://code.tutsplus...ok-app--net-471

Thanks again!!
Was This Post Helpful? 0
  • +
  • -

#5 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 1,816
  • Joined: 15-January 14

Re: Help can someone explain a PHP variable

Posted 12 March 2014 - 06:26 PM

When you have questions like that about specific things in PHP, you should look them up in the manual on php.net. This is the table of contents:

http://www.php.net/manual/en/

If you're new to PHP, pay special attention to the language reference section, especially the first 8 parts of it. If you have a question about something in particular, you can type it in to the search box on the top right. Here are the manual pages for rand and sizeof:

http://www.php.net/m...nction.rand.php
http://www.php.net/m...tion.sizeof.php

Those pages explain what the built-in functions do. For the "-1", the - is the subtraction operator, you can read about operators in the operators section of the language reference. The subtraction operator does exactly what you would think a subtraction operator would do.

This post has been edited by ArtificialSoldier: 12 March 2014 - 06:27 PM

Was This Post Helpful? 1
  • +
  • -

#6 asti80  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 28-February 14

Re: Help can someone explain a PHP variable

Posted 13 March 2014 - 03:21 PM

Thank U Artificial Soldier for the information, its just that sometimes I dont always understand the examples on the php.net
Thats why I need to ask, Im really sorry for this!

There are sometimes some samller parts in the code that I dont really understand and it usuaally important. Thats why I rather ask...

sorry and thanks again!
Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 1,816
  • Joined: 15-January 14

Re: Help can someone explain a PHP variable

Posted 13 March 2014 - 03:34 PM

I understand, but one of the most important skills that a programmer can have is the ability to do research. Out of everything that I learned while in college, the only thing I use every single day is being able to research. Everyone has questions about what they're doing or what they're working with, if you know how to answer your own questions then you will be much more effective in your work. Feel free to ask whatever you want to ask, but make sure that you understand that those answers are probably out there if you just go and look. Look what you get if you search for "php rand", for example:

https://www.google.c...=UTF-8&oe=UTF-8

That's a whole list of things including the manual that I linked you to, discussions about random number generators, etc. You are more than welcome to ask questions to help you learn, but asking someone else shouldn't be your first choice, there are a lot of resources out there. I'm not trying to dissuade you from asking questions though, I'm just trying to make the point that knowing how to do research is as important a skill as anything else.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1