php string comparison fails with self::mystring - php

The below class shows my situation - I am not getting the right results.
class Test {
public static $API_KEY = 'B0gTFoDazzV7e1EgutQg';
public static $API_SECRET = 'S5axjxfvpO2uNPocdXE';
public function test(){
$a= self::$API_KEY.":".self::$API_SECRET;
$'B0gTFoDazzV7e1EgutQg:S5ddjxfvpO2uNPocdXE';
if ($a==$b){
echo "True";
echo var_dump($a);
echo var_dump($b);
} else {
echo "False";
echo strlen($a);
echo strlen($b);
echo var_dump($a);
echo var_dump($b);
}
}
}
$a= new Test;
This should be the same! It should be true...
This should be TRUE!! any ideas/?>

Your two strings are not the same...
api_secret: S5axjxfvpO2uNPocdXEev (first part)
^^
compared to: S5ddjxfvpO2uNPocdXEev
^^

Related

Simple PHP class

I am learning php and created the below class, but I can't seem to figure out why it giving me the below errors which says:
144
Warning: Missing argument 1 for setters::set_a(), called in C:\xampp\htdocs\php\accessmod2.php on line 19 and defined in C:\xampp\htdocs\php\accessmod2.php on line 9
Notice: Undefined variable: value in C:\xampp\htdocs\php\accessmod2.php on line 11
<?php
class setters{
private $a = 144;
public function get_a(){
return $this->a;
}
public function set_a($value){
$this->a = $value;
}
}
$example = new setters();
echo $example->get_a()."<br />";
$example->set_a(15)."<br />";
echo $example->set_a()."<br />";
?>
You have to use a parameter for the set() function. But in your case, I think you just want to see if the set() function have work. So use the get() function.
So change to this :
echo $example->get_a()."<br />";
$example->set_a(15)."<br />";
echo $example->get_a()."<br />";
And the result is :
144
15
Check your last line:
echo $example->set_a()."<br />";
set_a() requires a parameter but it is empty. If you change it like this, it will work:
echo $example->set_a('someparameterhere')."<br />";
Your second call to ->set_a requires a parameter :
<?php
class setters{
private $a = 144;
public function get_a(){
return $this->a;
}
public function set_a($value){
$this->a = $value;
}
}
$example = new setters();
echo $example->get_a()."<br />";
$example->set_a(15)."<br />";
$example->set_a(23)."<br />"; // ◄■■■ PARAMETER FOR "SET_A".
?>
You can also use an "optional" parameter :
<?php
class setters{
private $a = 144;
public function get_a(){
return $this->a;
}
public function set_a( $value = -1 ){ // ◄■■■ OPTIONAL PARAMETER.
$this->a = $value;
}
}
$example = new setters();
echo $example->get_a()."<br />";
$example->set_a()."<br />"; // ◄■■■ OPTIONAL PARAMETER ($a = -1).
echo $example->get_a()."<br />"; // ◄■■■ NEW VALUE = -1.
?>
You are calling set_a twice.
After set, you need call get_a to show the value.
echo $example->get_a()."<br />";
$example->set_a(15)."<br />";
echo $example->get_a()."<br />";

Variable arrays in class context

I am trying to accomplish a simple class method where the user submit its name to a form and it returns a greeting message for every name on the variable array, such as "Welcome John", "Welcome Mike", etc...
Doing this as a regular function is easy:
$arr = array('Mike', 'John', 'Molly', 'Louis');
function Hello($arr) {
if(is_array($arr)) {
foreach($arr as $name) {
echo "Hello $name" . "<br>";
}
} else {
echo "Hello $arr";
}
}
Hello($arr);
However, I can't make it work in class context:
$arr = array('Mike', 'John', 'Molly', 'Louis');
class greetUser {
public $current_user;
function __construct($current_user) {
$this->current_user = $current_user;
}
public function returnInfo() {
if(is_array($this->current_user)) {
foreach($this->current_user as $name) {
echo "Welcome, " . $name;
}
} else {
echo "Welcome, " . $this->current_user;
}
}
}
$b = new greetUser(''.$arr.'');
$b->returnInfo();
replace your $b = new greetUser(''.$arr.''); with $b = new greetUser($arr); and it will work :)
I was commiting a very silly mistake, as users pointed out, I was concatenating the variable when it was not necessary!

Checking for undefined variables in a function php

So I've been trying to devise a function that will echo a session variable only if it is set, so that it wont create the 'Notice' about an undefined variable. I am aware that one could use:
if(isset($_SESSION['i'])){ echo $_SESSION['i'];}
But it starts to get a bit messy when there are loads (As you may have guessed, it's for bringing data back into a form ... For whatever reason). Some of my values are also only required to be echoed back if it equals something, echo something else which makes it even more messy:
if(isset($_SESSION['i'])){if($_SESSION['i']=='value'){ echo 'Something';}}
So to try and be lazy, and tidy things up, I have tried making these functions:
function ifsetecho($variable) {
if(!empty($variable)) {
echo $variable;
}
}
function ifseteqecho($variable,$eq,$output) {
if(isset($variable)) {
if($variable==$eq) {
echo $output;
}
}
}
Which wont work, because for it to go through the function, the variable has to be declared ...
Has anyone found a way to make something similar to this work?
maybe you can achieve this with a foreach?
foreach ($_SESSION as $variable)
{function ifseteqecho($variable,$eq,$output) {
if($variable==$eq) {
echo $output;
}
else echo $variable;
}
}
now this will all check for the same $eq, but with an array of corresponding $eq to $variables:
$equiv = array
('1'=>'foo',
'blue'=>'bar',);
you can check them all:
foreach ($_SESSION as $variable)
{function ifseteqecho($variable,$equiv) {
if(isset($equiv[$variable])) {
echo $equiv[$variable];
}
else {
echo $variable;
}
}
}
Something like this?, you could extend it to fit your precise needs...
function echoIfSet($varName, array $fromArray=null){
if(isset($fromArray)){
if(isset($fromArray[$varName])&&!empty($fromArray[$varName])){
echo $fromArray[$varName];
}
}elseif(isset($$varName)&&!empty($$varName)){
echo $$varName;
}
}
You may use variable variables:
$cat = "beautiful";
$dog = "lovely";
function ifsetecho($variable) {
global $$variable;
if(!empty($$variable)){
echo $$variable;
}
}
ifsetecho("cat");
echo "<br/>";
ifsetecho("dog");
echo "<br/>";
ifsetecho("elephant");
UPDATE: With a rather complex code I’ve managed to meet your requirements:
session_start();
$cat = "beautiful";
$dog = "lovely";
$_SESSION['person']['fname'] = "Irene";
function ifsetecho($variable){
$pattern = "/([_a-zA-Z][_a-zA-Z0-9]+)".str_repeat("(?:\\['([_a-zA-Z0-9]+)'\\])?", 6)."/";
if(preg_match($pattern, $variable, $matches)){
global ${$matches[1]};
if(empty(${$matches[1]})){
return false;
}
$plush = ${$matches[1]};
for($i = 2; $i < sizeof($matches); $i++){
if(empty($plush[$matches[$i]])){
return false;
}
$plush = $plush[$matches[$i]];
}
echo $plush;
return true;
}
return false;
}
ifsetecho("cat");
echo "<br/>";
ifsetecho("dog");
echo "<br/>";
ifsetecho("elephant");
echo "<br/>";
ifsetecho("_SESSION['person']['fname']");
echo "<br/>";
ifsetecho("_SESSION['person']['uname']");
echo "<br/>";

Variable inside the link

Would someone be able to help me with some php.
I am new to this and I am trying to solve the puzzle.
I am trying to combine the input data that user has provided with the link so that final output displays record for the user whose regid was provided by user via input text field.
Here is some code I came up with that obviously does not work.
class Fields_View_Helper_FieldStats extends Fields_View_Helper_FieldAbstract
{
public function fieldStats($subject, $field, $value)
{
$userid = preg_replace(trim($value->value));
// create user's profile address using their username/userid
$stats = $userid;
echo '<div style="margin:0px auto;"><script type="text/javascript" src="http://e1.statsheet.com/embed/';
return $this->view->string()->chunk($value->value);
echo '/1/NuNes.js"></script></div>';
}
}
To concatenate a string in PHP do this (note the periods, which are doing the work)
$str = "Line 1 " . $somevar . " Line 2";
return $str
Issuing a return terminates your function. I would build one string inside a variable then return that variable
The return ends the method, because it returns the value to the caller.
<?php
function fn() {
return "bar";
}
echo fn(); // will output bar
function fn2() {
echo "foo";
return "bar";
}
echo fn2(); // will output foobar
function fn3() {
return "foo" . fn();
}
echo fn3(); // will output foobar as well
?>
And here's how you can connect those three lines in the code snippet you posted:
<?php
class Fields_View_Helper_FieldStats extends Fields_View_Helper_FieldAbstract
{
public function fieldStats($subject, $field, $value)
{
$userid = preg_replace(trim($value->value));
// create user's profile address using their username/userid
$stats = $userid;
return
'<div style="margin:0px auto;"><script type="text/javascript" src="http://e1.statsheet.com/embed/' .
$this->view->string()->chunk($value->value) .
'/1/NuNes.js"></script></div>'
;
}
}
?>
And here's how you can concatenate strings:
<?php
$string1 = 'foo ' . fn() . ' bar';
$string2 = "foo 2" . fn() . " bar";
?>
And here's how you can embed stuff in variables (faster):
<?php
$string1 = fn();
$string1 = "foo {$string1} bar";
// Or with an object
class Foo {
public function fn(){}
}
$foo = new Foo();
$string1 = "foo {$foo->fn()} bar";
?>

What should be passed into if() to print 'Hello World'?

What should be passed into the if() to print the output as "Hello World"? [Note: It should execute the else block.]
if(?){
} else {
echo "World";
}
I needs to evaluate to false, and print "Hello" at the same time. printf returns the length of outputted string upon success which is evaluated to true when read in a Boolean context. So reversing that will evaluate to false, executing the else block.
if(!printf("Hello ")){
} else {
echo "World";
}
!printf("Hello ")
By default, printf in 'C' returns true.
if(!printf("Hello "))
{}
else
{
echo "World";
}
You can do in this way...
There is also an alternate solution for this question:
class test{
function __construct()
{
echo "Hello";
}
}
if(!new test){
}else{
echo "World";
}
Anything that evaluates to FALSE.
if understands logical result, i mean TRUE-FALSE
so any any condition that results in true/false results matters for if so you can use
if(true){
echo 'this is executed';
}else{
echo "world";
}
OR
if(false){
echo 'this is executed';
}else{
echo "world";
}
i hope this works
if(printf("Hello ")) {
}
else{
echo "World";}
i think this is enough.....sorry if not

Categories