View Full Version : Echoing Variables.
saeed
12-11-2007, 09:04 AM
I've just start using PHP and am doing practice through online tutorial at tizag.com. I want to know difference between String and Variable. Correct me if am wrong .. we can store text values in string but not the integer (numbers 0-9) value similarly in Integers can be stored in Variables but variables can't store text. :confused: am entirely confused in it.
String = a-z A-Z
Variable = 0-9
what about special characters and which one are allowed in PHP?
see the code below
$my_letter = a; its not within double quotes ... thts why its integer value but the value is 'a' ??? this is not cleared.
It would be very much appreciated if someone can provide me a link for brief tutorial ... esp. Variable and String concepts.
<html>
<head>
<title>Working with Echoing Quotes</title>
</head>
<body>
<?php
$my_string = "Hello Saeed. My name is: ";
$my_number = 4;
$my_letter = a;
echo $my_string;
echo $my_number;
echo $my_letter;
?>
</body>
</html>
matak
12-11-2007, 09:15 AM
you mean string or integer
string is "text" and integer is number.
variable can be both string or integer.
you need to escape special chars if you use double quotes for echo
example
echo "<a href=\"mylink.html\">This is my link</a>";
but if you use single quotes you can write
echo '<a href="mylink.html">This is my link</a>'
saeed
12-11-2007, 09:24 AM
:thumbsup:
Thank you very much.
btw... I was unable to find "Thanks for this post" link.
matak
12-11-2007, 09:35 AM
no prob mate. i learned my lesson about reputation :D
if you got questions, just post 'em here ;)
saeed
12-11-2007, 10:01 AM
rep approved! :)
<html>
<head>
<title>Working with heredoc</title>
</head>
<body>
<?php
$my_string = <<<TEST
Tizag.com
Webmaster Tutorials
Unlock your potential!
TEST;
echo $my_string;
?>
</body>
</html>
We've got string named as $my_string but value is in multiple lines output of the above code is like this:
Tizag.com Webmaster Tutorials Unlock your potential!
and its not cleared coz wht does <<<TEST mean and at the end TEST; I donno wht it is.
sorry am almost new to this language.
thanks anyway,
Saeed.
matak
12-11-2007, 10:22 AM
you can use <<< it if you have to write lot's of html code in your php and for some reason you don't want to use " or ' quotes.
some people use it often some people never use it. later when you learn PHP more, you will realize that you basicly never write any html code inside PHP itself, but most often use .tpl files where your html is written.
just look at phpbb3, and how it's written ;)
saeed
12-11-2007, 12:01 PM
sorry to disturb you again.
I need some simple examples of
+=
-=
*=
/=
%=
.=
Thank you
CaptainB
12-11-2007, 12:16 PM
Maybe this will help you: http://www.tizag.com/phpT/operators.php
matak
12-11-2007, 12:33 PM
they are called operators (http://hr.php.net/manual/en/language.operators.php). you can find explanations on php.net website
saeed
12-11-2007, 12:53 PM
This is wht I've done so far.
<?php
$x = 2;
$y = 3;
echo "If 2 is equal to 3? <br>";
if ($x == $y){
echo "Yes! Condition Operator is true <br><br>";
}else{
echo "No! Condition Operator is false <br><br>";
}
echo "If 2 is not equal to 3? <br>";
if ($x != $y){
echo "Yes! Condition Operator is true <br><br>";
}else{
echo "No! Condition Operator is false <br><br>";
}
echo "If 2 is less than 3? <br>";
if ($x < $y){
echo "Yes! Condition Operator is true <br><br>";
}else{
echo "No! Condition Operator is false <br><br>";
}
echo "If 2 is greater than 3? <br>";
if ($x > $y){
echo "Yes! Condition Operator is true <br><br>";
}else{
echo "No! Condition Operator is false <br><br>";
}
echo "If 2 is less or equal to 3? <br>";
if ($x <= $y){
echo "Yes! Condition Operator is true <br><br>";
}else{
echo "No! Condition Operator is false <br><br>";
}
echo "If 2 is greater or equal to 3? <br>";
if ($x >= $y){
echo "Yes! Condition Operator is true <br><br>";
}else{
echo "No! Condition Operator is false <br><br>";
}
?>
I've got a query ... I want correct indentation, use of comments .. u know little things to keep your code neat n clean. Its good to get used to of it in early development. Is there any guide or article related to it?
matak
12-11-2007, 01:30 PM
comments used wrong
//this is piece of code that sets variable x to value number 5 that will later be
//used for multiplying it with variable y to get variable z value
x=5;
comments used right
//function used to display mysql results for database user
function showUsers() {
//pieces of code that go here
}
saeed
12-12-2007, 05:22 AM
thanks for correcting me.
I was looking source in Firefox of the above PHP Code
and all html output was in single line. I mean the whole body line was in single line.
to me, it shouldn't be like this. u know why its showing me like this ?
PHP comment are not shown in Source unlike HTML comments.
If i start my PHP document with <?php
and then I start my remaining html code tht is .. all html, head, body... tags after <?php is tht right practice of using PHP?
thanks man,
Inigoesdr
12-12-2007, 05:35 AM
HTML comments are used like this: <!-- comment -->
And your HTML code output is on a single line because you have no newlines in your code. You can either: echo 'text
text';
// or
echo "text\n\ntext";
Keep in mind that HTML ignores extra spaces and newlines so to drop to the next line in HTML you have to use the <br /> tag.
saeed
12-12-2007, 07:09 AM
thanks for your reply Inigoesdr,
Can u teach me Global Variables. :confused:. I am not getting it at all.
<?php
$my_data = "Outside data ";
function send_data(){
global $my_data;
echo $my_data;
}
send_data();
echo $my_data;
?>
Now this is wht I understand till now.
Variable declared $my_data and its value Outside data
function open send_data()
global variable declared $my_data
print $my_data
function closed
calling function send_data
print $my_data
Whts the point of calling global variable within a function.. Can we declare global variable outside function? Beginning PHP by Wrox is totally confusing. :confused:
and secondly, what are pseudo codes? can u give me example Please.
Thank you
Inigoesdr
12-12-2007, 07:21 AM
global (http://php.net/global) allows you to use a variable that is outside of the function's scope. Pseudo code is the name for a simplified code snippet that is just used to explain how the code's execution would work; it not real code that could be used.
saeed
12-12-2007, 08:01 AM
<?php
$foo = 'Bob'; // Assign the value 'Bob' to $foo
$bar = &$foo; // Reference $foo via $bar.
$bar = "My name is $bar"; // Alter $bar...
echo $bar;
echo $foo; // $foo is altered too.
?>
honestly am not getting it at all... to me .. output should be like this.
My name is BobBob
can anyone explain the above example to me Please :confused:
Thank you
abduraooft
12-12-2007, 08:55 AM
$foo='Bob';
$foo.=$foo;
$bar= "my name is $foo";
echo $bar; ??
Inigoesdr
12-12-2007, 09:53 AM
<?php
$foo = 'Bob'; // Assign the value 'Bob' to $foo
$bar = &$foo; // Reference $foo via $bar.
$bar = "My name is $bar"; // Alter $bar...
echo $bar;
echo $foo; // $foo is altered too.
?>
honestly am not getting it at all... to me .. output should be like this.
My name is BobBob
can anyone explain the above example to me Please :confused:
Thank you
The output is My name is BobMy name is Bob because:
Line 1: assigns 'Bob' to $foo
Line 2: Sets $bar to point(or reference) to $foo
Line 3: Assigns "My name is Bob" to $bar, and in turn to $foo since they reference the same thing
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.