echo php variable and html in Wordpress - php

I am playing around with wordpress and wondering, why this line of code works:
echo "<a href='....'>$name</a>";
I learned it this way:
echo "<a href='....'>".$name."</a>";
Is there something special defined in WP to make this work?

In PHP, there are two types of String.
The first type uses the single quotes, as follows.
$val = 'this is a simple string';
The second type is as follows:
$val = "This is a not so simple string";
With the latter type, any variables included in the string will be resolved to their values, so:
$val = "Hello there";
$message = 'Dave says $val';
// Literally equals: Dave says $val
$message2 = "Dave says $val";
// Literally equals: Dave says Hello there
There are lots of other differences, which you can read about here.

Related

php print statements in separate lines

The following php script prints several statements and variables! they print on the sameline! i want to print them in separate lines one after the other! How can i do it?
for example the 1st 2 statements should be printed as:
Programming in PHP is fun
I learnt how to comment in PHP
but currently it prints as
Programming in PHP is fun I learnt how to comment in PHP
?php
echo "Programming in PHP is fun";
//This is the 1st programming 4 tutorial
/*This tutorial helped me
to gain a good knowledge in php! */
print "I learnt how to comment in PHP";
echo "A variable name must start with a letter or an underscore '_' -- not a number";
echo "A variable name can only contain alpha-numeric characters, underscores (a-z, A-Z, 0-9, and _ )";
echo "A variable name should not contain spaces. If a variable name is more than one word, it should beseparated with an underscore or with capitalization ";
$name= "My name is XXX";
$number=25;
$float_number=10.245;
$negative_number=-12;
echo($name);
echo($number);
echo($float_number);
echo($negative_number);
$age=24;
$feature =$age;
echo($feature);
$x=45;
$y=12;
echo($x+$y);
$myName="My name is 'Lasith'";
?>
If you echo them as plain text, add a newline character after each echo:
echo "\n";
If you echo them to HTML (into a web browser), add a HTML <br> tag after each echo:
echo "<br>";
You can also use string concatenation
echo "Programming in PHP is fun" . "\n";
echo "Programming in PHP is fun" . "<br>";
or add the newline or <br> tag to the string
echo "Programming in PHP is fun\n";
echo "Programming in PHP is fun<br>";

escape character not working as expected

I am trying to create html content in PHP and for onclick event I have included a function named uchat for a div. The function takes a name parameter which is a string.
Like below:
$name = "Php string";
$echostr .= "<div onClick='uchat(\'$name\')'>
</div>";
But, passing a string value like this causes syntax error when div is clicked. Because, single quote is within a single quote. I have tried to escape it, but it still doesnt work.
The error is this:
SyntaxError: illegal character
uchat(\
I am not sure how to escape a string parameter and I have come across this problem so many times, Please help if you have a solution for this.
Thanks.
Escaped single quotes will conflict with outer ones:
$echostr .= "<div onClick=\"uchat('$name')\">
</div>";
Here are 2 clean and simple ways to do this:
1. Classic concat
$name = "Php string";
$str = "<div onClick=\"uchat('" . $name . "')\"></div>";
print $str;
2. Using sprintf (http://us3.php.net/manual/en/function.sprintf.php)
$name = "Php string2";
$str = sprintf("<div onClick=\"uchat('%s')\"></div>", $name);
print $str;
try like this
$echostr .= "<div onClick='uchat("$name")'></div>";
This works:
<?php
$name = "Php string";
$echostr .= <<< EOF
<div onClick="uchat('$name')"></div>
EOF;
echo $echostr;
?>
Output:
<div onClick="uchat('Php string')"></div>
In order to avoid escaping all double quotes and to make the html code more readable you can use EOF.
See it action : http://ideone.com/vRCCVH

Issues while using Quotes in PHP

I have Learnt that Quotes doesn't matter in PHP.
But in the following code, if I try to use single quotes in eval(); I get error, on the other hand code works fine with Double Quotes.
$a = '2';
$b = '3';
$c = '$a+$b';
echo $c.'<br/>';
eval("\$c = \"$c\";");
//eval('\$c = \'$c\';'); //Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING
echo $c;
PHP.net says that escape sequences are not expanded when using single quotes.
Quotes do matter ;-)
<?php
$color = "red";
echo "My car is $color"; // Outputs "My car is red"
echo 'My car is $color'; // Outputs "My car is $color"
?>
Unlike double quotes, PHP does not parse variables in single quotes.
Example:
$name = 'John';
echo 'hello $name'; // hello $name
echo "hello $name"; // hello John
More Information
FYI, it isn't good idea to use eval in production environment for security reasons.
Using eval is a bad idea but if you are doing this for learning purpose then the correct way is
eval("\$c = \$c;");
.
don't use eval and update your string-quoting skills here:
The following example was lifted from: The PHP Manual
<?php
echo 'this is a simple string';
echo 'You can also have embedded newlines in
strings this way as it is
okay to do';
// Outputs: Arnold once said: "I'll be back"
echo 'Arnold once said: "I\'ll be back"';
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\\*.*?';
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\*.*?';
// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline';
// Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either';
?>

Getting rid of escape characters in PHP

I've got a string containing values such as "hello world\' hello world\'" and I'd like to get rid of the escape characters (the backslashes.)
I've tried the following code:
str_replace("\\", "", $data);
But it doesn't seem to work.
If all you want to do is to get rid of backslashes, then there's a very handy PHP function that accomplishes just that
$var = stripslashes($var);
Assuming you're using $var as the last parameter in str_replace() instead of $data, it should work fine.
$var = "hello world\' hello world\'";
echo $var . "<br />";
echo str_replace("\\", "", $var) . "<br />";
Output:
hello world\' hello world\'
hello world' hello world'
this should work great for you you were not referencing the variable $var correctly in php replace subject parameter also assuming you need to replace the \' you were putting \ which searches for it hence nothing was found to be replaced hope this helps
$var = "hello world\' hello world\'";
echo str_replace("\'","",$var);

What is the difference between double and single quotes in PHP? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Difference between single quote and double quote string in php
I am new to PHP and in programming i have seen use of both "" and ' '.
What is the difference between "" and ' '?
And in declaring a link i have used the following ,but it does not seem to work there must be something wrong in quotation:
$abc_output .='' Back to Main Menu'';
echo $abc_output;
What might be the error here?
You want to keep the text inside of your string:
$abc_output .='Back to Main Menu';
The difference between ' and " is that you can embed variables inside of a double quoted string.
For example:
$name = 'John';
$sentence = "$name just left"; // John just left
If you were to use single quotes, then you'd have to concatenate:
$name = 'John';
$sentence = $name.' just left'; // John just left
PS: Don't forget that you always have to escape your quotes. The following 2 are the same:
$double = "I'm going out"; // I'm going out
$single = 'I\'m going out'; // I'm going out
Same applies the other way around:
$single = 'I said "Get out!!"'; // I said "Get out!!"
$double = "I said \"Get out!!\""; // I said "Get out!!"
Double quotes allow additional expressions, such as "$variable \n", which single quotes don't. Compare:
$variable = 42;
echo "double: $variable,\n 43\n";
echo 'single: $variable,\n 43';
This outputs:
double: 42,
43
single: $variable,\n 43
For more information, refer to the official documentation.
Text in double quotes are parsed.
For example:
$test = 'something';
print('This is $test');
print("This is $something");
would result in:
This is $test
This is something
If you don't need the string to be parsed you should use single quotes since it's better performance wise.
In your case you need to do:
$abc_output .='Back to Main Menu';
echo $abc_output;
Or you will get an error.
The Back to Main Menu wasn't in the string.

Categories